是否可以调用不是从按钮调用命令

本文关键字:调用 按钮 命令 是否 | 更新日期: 2023-09-27 18:36:47

DataGrid 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>  

当用户单击DataTemplate的任何地方(在标题模板范围内)调用按钮Command="{Binding DataContext.HelloCommand, RelativeSource={RelativeSource AncestorType=Window}}"时,是否可以调用?

是否可以调用不是从按钮调用命令

基本思想是为此使用附加的路由事件。以下是这方面的代码片段:

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

路由事件触发器

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;
    }
}

自定义命令操作

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);
                    }
                }
            }
        }
    }
}