WPF事件到命令的另一个实现(有问题)



我正在查看将iccommand连接到控件事件的各种实现。例如,TextBox的GotFocus应该在我的视图模型中调用GotFocusCommand。然后我有了一个想法来实现我自己的版本(为了我自己的学习),它工作得很好,但我只能将一个事件链接到XAML中的一个命令。

(基本上我只是使用反射来找到指定的事件,然后做一个执行命令的AddEventHandler)

<Button 
  local:EventToCommand.Event="Click" 
  local:EventToCommand.Command="{Binding TestCommand}" 
  />  

但这不是:

<Button 
  local:EventToCommand.Event="Click" 
  local:EventToCommand.Command="{Binding ClickCommand}" 
  local:EventToCommand.Event="GotFocus" 
  local:EventToCommand.Command="{Binding GotFocusCommand}"
  />  

,因为它会导致重复的属性名错误。

可不可以这样做:

<Button>
  <Some Xaml Element>
    <local:EventToCommand Event="Click" Command="{Binding ClickCommand}" />
    <local:EventToCommand Event="GotFocus" Command="{Binding GotFocusCommand}" />
  </Some Xaml Element>
</Button>

将多个事件"映射"到命令?

有几种方法可以做到这一点,要么使用附加属性,要么继承Button并添加自己的DependencyProperty,其中包含EventToCommand对象列表,当您添加到该集合时,您将事件连接到命令。如果这看起来令人困惑,我可以试着举出一些例子。

c#

    public class EventedButton : Button
{
    public static DependencyProperty EventCommandsProperty
        = DependencyProperty.Register("EventCommands", typeof(EventToCommandCollection), typeof(EventedButton), new PropertyMetadata(null));

    public EventToCommandCollection EventCommands
    {
        get
        {
            return this.GetValue(EventCommandsProperty) as EventToCommandCollection;
        }
        set
        {
            this.SetValue(EventCommandsProperty, value);
        }
    }
    public EventedButton()
    {
        this.EventCommands = new EventToCommandCollection(this);
    }
}
Xaml:

    <local:EventedButton>
        <local:EventedButton.EventCommands>
            <local:EventToCommand />
        </local:EventedButton.EventCommands>
    </local:EventedButton>

在EventToCommandCollection中,当项目被添加到集合中时,你可以附加/分离到你想要的事件。

更新:附加属性

下面是一些将集合作为附加属性的代码: c#

        public static DependencyProperty CommandsProperty =
        DependencyProperty.RegisterAttached(
        "Commands",
        typeof(ICollection<EventToCommand>),
        typeof(DependencyObject),
        new PropertyMetadata(null, OnCommandsChanged));

    private static void OnCommandsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Attach/Detach event handlers
    }
    public static void SetCommands(DependencyObject element, ICollection<EventToCommand> value)
    {
        element.SetValue(CommandsProperty, value);
    }
    public static ICollection<EventToCommand> GetCommands(DependencyObject element)
    {
        return (ICollection<EventToCommand>)element.GetValue(CommandsProperty);
    }
Xaml:

    <local:EventedButton>
        <local:EventToCommand.Commands>
            <local:EventToCommandCollection>
                <local:EventToCommand/>
            </local:EventToCommandCollection>
        </local:EventToCommand.Commands>
    </local:EventedButton>

使用混合事件触发器和操作否定了处理自己的集合的需要。并且可以添加到任何控件中。

参见MVVM Lights EventToCommand

或者我在这里的扩展。(来源)

GalaSoft MVVM Light ToolKit - EventToCommand你可以这样做

<Button> 
  <i:Interaction.Triggers>
     <i:EventTrigger EventName="Click" >
      <cmd:EventToCommand 
          PassEventArgsToCommand="True"
           Command="{Binding ButtonClick}"
       />
    </i:EventTrigger>
 </i:Interaction.Triggers>
  <i:Interaction.Triggers>
     <i:EventTrigger EventName="GotFocus" >
      <cmd:EventToCommand 
          PassEventArgsToCommand="True"
           Command="{Binding ButtonGotFocus}"
       />
    </i:EventTrigger>
 </i:Interaction.Triggers>
</Button>

 i- xmlns:i="clr-namespace:System.Windows.Interactivity;
    assembly=System.Windows.Interactivity"
 cmd-xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;
     assembly=GalaSoft.MvvmLight.Extras.WPF4"

最新更新