Search code examples
c#wpfmvvmcommanddatatemplate

Is it possible to invoke Command not from Button?


I have DataGrid with DataTemplate:

<DataGrid ItemsSource="{Binding Persons}" Grid.Row="1" AutoGenerateColumns="False"> 
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding IdPerson}">                    
       <DataGridTextColumn.HeaderTemplate>                        
          <DataTemplate>
             <Grid>
                <Grid.RowDefinitions>
                   <RowDefinition/>
                   <RowDefinition/>
                   <RowDefinition/>
                </Grid.RowDefinitions>
               <Button DataContext="{Binding Path=Data, Source={StaticResource proxy}}" 
Command="{Binding DataContext.HelloCommand, RelativeSource={RelativeSource  
AncestorType=Window}}"/>
               <TextBlock Grid.Row="1" HorizontalAlignment="Center" Text = "{Binding 
DataContext.Hello, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>                                                   
            </Grid>                            
         </DataTemplate>
      </DataGridTextColumn.HeaderTemplate>                   
   </DataGridTextColumn>
   <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}"/>
   <DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/>
   </DataGrid.Columns>
</DataGrid>  

Is it possible when user clicks at any place(in scope of HeaderTemplate) of DataTemplate to invoke Command="{Binding DataContext.HelloCommand, RelativeSource={RelativeSource AncestorType=Window}}" of Button?


Solution

  • Basic idea is to use attached routed event for this. Here are code snippets for this:

            <i:Interaction.Triggers>
                <local:RoutedEventTrigger RoutedEvent="Mouse.MouseDown">
                    <local:CustomCommandAction Command="{Binding DataContext.HelloCommand, RelativeSource={RelativeSource AncestorType=Window}}" />
                </local:RoutedEventTrigger>
            </i:Interaction.Triggers>
    

    RoutedEventTrigger

    public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
    {
        RoutedEvent _routedEvent;
    
        public RoutedEvent RoutedEvent
        {
            get { return _routedEvent; }
            set { _routedEvent = value; }
        }
    
        public RoutedEventTrigger()
        {
        }
        protected override void OnAttached()
        {
            Behavior behavior = base.AssociatedObject as Behavior;
            FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
    
            if (behavior != null)
            {
                associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
            }
            if (associatedElement == null)
            {
                throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
            }
            if (RoutedEvent != null)
            {
                associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
            }
        }
        void OnRoutedEvent(object sender, RoutedEventArgs args)
        {
            base.OnEvent(args);
        }
        protected override string GetEventName()
        {
            return RoutedEvent.Name;
        }
    }
    

    CustomCommandAction

    public sealed class CustomCommandAction : TriggerAction<DependencyObject>
    {
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomCommandAction), null);
    
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof(ICommand), typeof(CustomCommandAction), null);
    
        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }
            set
            {
                this.SetValue(CommandProperty, value);
            }
        }
    
        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }
    
            set
            {
                this.SetValue(CommandParameterProperty, value);
            }
        }
    
        protected override void Invoke(object parameter)
        {
            if (this.AssociatedObject != null)
            {
                ICommand command = this.Command;
                if (command != null)
                {
                    if (this.CommandParameter != null)
                    {
                        if (command.CanExecute(this.CommandParameter))
                        {
                            command.Execute(this.CommandParameter);
                        }
                    }
                    else
                    {
                        if (command.CanExecute(parameter))
                        {
                            command.Execute(parameter);
                        }
                    }
                }
            }
        }
    }