问题描述
我正在考虑将一个ICommand挂起到一个控件的事件的各种实现。因此,例如TextBox的GotFocus应该在View Model中调用GotFocusCommand。然后我有一个想法来实现我自己的版本(为了我自己的学习),它的工作正常,但我只能将一个事件链接到XAML中的一个命令。
这样可以正常工作:
< Button
local:EventToCommand.Event =点击
local:EventToCommand.Command ={Binding TestCommand}
/>
但这不是:
< Button
local:EventToCommand.Event =点击
local:EventToCommand.Command ={Binding ClickCommand}
local:EventToCommand.Event = GotFocus
local:EventToCommand.Command ={Binding GotFocusCommand}
/>
,因为它会导致重复的属性名称错误。
是否可以执行以下操作:
< Button>
< Some Xaml Element>
< local:EventToCommand Event =点击Command ={Binding ClickCommand}/>
< local:EventToCommand Event =GotFocusCommand ={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
{
将this.GetValue(EventCommandsProperty)作为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内部,当将项目添加到集合中时,您将附加/分离到您想要的事件
更新:附加属性
以下是将集合作为附加属性执行的一些代码: p>
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)
{
//附加/分离事件处理程序
}
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>
I am looking at various implementations of hooking a ICommand up to a control's event. So for instance the GotFocus of a TextBox should call a GotFocusCommand in my View Model. I then got an idea to implement my own version (for my own learning) and it is working well, but I can only link one event to one command in the XAML.
( Basically I just use reflection to find the specified Event and then do a AddEventHandler that executes the command )
This works fine :
<Button
local:EventToCommand.Event="Click"
local:EventToCommand.Command="{Binding TestCommand}"
/>
But this does not :
<Button
local:EventToCommand.Event="Click"
local:EventToCommand.Command="{Binding ClickCommand}"
local:EventToCommand.Event="GotFocus"
local:EventToCommand.Command="{Binding GotFocusCommand}"
/>
as you it leads to a duplicate attribute name error.
Would it be possible to do something like :
<Button>
<Some Xaml Element>
<local:EventToCommand Event="Click" Command="{Binding ClickCommand}" />
<local:EventToCommand Event="GotFocus" Command="{Binding GotFocusCommand}" />
</Some Xaml Element>
</Button>
to "map" multiple events to commands ?
There are a couple of ways you could approach this, either using an Attached Property or inheriting from Button and adding your own DependencyProperty that contains a list of EventToCommand objects, and when you add to that collection you wire up the event to command. If this seems confusing, I can try to whip up some examples.
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>
Inside of EventToCommandCollection, you would attach/detach to the Event you wanted when items are added to the collection.
UPDATE: Attached Property
Here is some code to do the collection as an attached property:
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>
这篇关于另一个执行WPF事件到命令(有问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!