我如何添加一个的ICommand从FrameworkElement的事件? [英] How can I add a ICommand to an event from FrameworkElement?
问题描述
我如何添加一个的ICommand
从 FrameworkElement的
?
$事件b $ b
具体来说,我想要做以下
<预类=郎咸平的XML prettyprint-覆盖>
< ListView的> <! - 巴顿,Windows或任何元素 - >
<我:EventToCommand
NAME =PreviewMouseLeftButton
命令={绑定路径= MyCommand}
CommandParameter ={绑定...}/>
<! - 价值元素,等等。 - >
< /&的ListView GT;
我要实现我自己的解决方案,这是对教育目的,我不希望使用任何第三方库(MVVM光,棱镜等)
您需要使用附加行为。出于演示的目的,说我想要实现使用MVVM和ICommand的模式按钮双击,这里的相关代码:
首先,创建一个静态类称为ButtonBehaviors看起来像这样的:
公共静态类ButtonBehaviors
{
公共静态对象GetButtonDoubleClick(DependencyObject的OBJ)
{
返回obj.GetValue(ButtonDoubleClickProperty);
}
公共静态无效SetButtonDoubleClick(OBJ的DependencyObject,对象的值)
{
obj.SetValue(ButtonDoubleClickProperty,值);
}
公共静态只读的DependencyProperty ButtonDoubleClickProperty =
DependencyProperty.RegisterAttached(ButtonDoubleClick的typeof(对象)的typeof(ButtonBehaviors),
新UIPropertyMetadata(新PropertyChangedCallback (OnButtonDoubleClickChanged)));
私有静态无效OnButtonDoubleClickChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
VAR键= D的按钮;
如果(键== NULL)
{
的回报;
}
VAR命令= e.NewValue为ICommand的;
如果(命令== NULL)
{
的回报;
}
button.MouseDoubleClick + =(O,EV)=> command.Execute(按钮);
}
}
(我会在几秒钟之内解释)
那么,这里是你将如何使用它:
MainWindow.xaml:
<窗口x:类=WpfApplication3.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml的xmlns:WpfApplication3 =CLR的命名空间:WpfApplication3标题=主窗口HEIGHT =350WIDTH = 525>
<网格和GT;
<按钮内容=按钮
高度=23
的HorizontalAlignment =左
保证金=173,89,0,0
VerticalAlignment =评出的
WpfApplication3:ButtonBehaviors.ButtonDoubleClick ={结合ButtonDoubleClick}
WIDTH =75/>
< /网格和GT;
< /窗GT;
最后,视图模型:
公共类视图模型
{
私人ICommand的_buttonDoubeClick;
公众的ICommand ButtonDoubleClick
{
得到
{
如果(_buttonDoubeClick == NULL)
{
_buttonDoubeClick =新SimpleDelegateCommand (()=> MessageBox.Show(双击!!));
}
返回_buttonDoubeClick;
}
}
}
和刚才的完整性,因为你说没有第三方DLL的,这里是我SimpleDelegateCommand:
公共类SimpleDelegateCommand:ICommand的
{
私人只读行动_action;
公共SimpleDelegateCommand(动作动作)
{
_action =行动;
}
公共BOOL CanExecute(对象参数)
{
返回真;
}
公共事件的EventHandler CanExecuteChanged;
公共无效执行(对象参数)
{
如果(_action!= NULL)
{
_action();
}
}
}
在概括地说发生了什么是这样的:当你指定命令将附加属性,在OnButtonDoubleClickChanged事件获取提出,在这一点上,我们挂接到button.MouseDoubleClick事件
How can I add an ICommand
to an event from FrameworkElement
?
Specifically I want to do the following
<ListView> <!-- Button, Windows or whatever element -->
<my:EventToCommand
Name="PreviewMouseLeftButton"
Command="{Binding Path=MyCommand}"
CommandParameter="{Binding ...}" />
<!-- Value, Element, etc.. -->
</ListView>
I want to implement my own solution, which is for educational purposes and I don't wish to use any third-party libraries (MVVM Light, Prism, etc)
You need to use Attached Behaviors. For demonstration purposes, say I wanted to implement the Button Double Click using MVVM and the ICommand pattern, here's the relevant code:
First, create a static class called ButtonBehaviors that looks like this:
public static class ButtonBehaviors
{
public static object GetButtonDoubleClick(DependencyObject obj)
{
return obj.GetValue(ButtonDoubleClickProperty);
}
public static void SetButtonDoubleClick(DependencyObject obj, object value)
{
obj.SetValue(ButtonDoubleClickProperty, value);
}
public static readonly DependencyProperty ButtonDoubleClickProperty =
DependencyProperty.RegisterAttached("ButtonDoubleClick", typeof (object), typeof (ButtonBehaviors),
new UIPropertyMetadata(new PropertyChangedCallback(OnButtonDoubleClickChanged)));
private static void OnButtonDoubleClickChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = d as Button;
if(button == null)
{
return;
}
var command = e.NewValue as ICommand;
if(command == null)
{
return;
}
button.MouseDoubleClick += (o, ev) => command.Execute(button);
}
}
(I'll explain in a sec)
Then, here's how you would use it:
MainWindow.xaml:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button"
Height="23"
HorizontalAlignment="Left"
Margin="173,89,0,0"
VerticalAlignment="Top"
WpfApplication3:ButtonBehaviors.ButtonDoubleClick="{Binding ButtonDoubleClick}"
Width="75" />
</Grid>
</Window>
Finally, the ViewModel:
public class ViewModel
{
private ICommand _buttonDoubeClick;
public ICommand ButtonDoubleClick
{
get
{
if (_buttonDoubeClick == null)
{
_buttonDoubeClick = new SimpleDelegateCommand(() => MessageBox.Show("Double click!!"));
}
return _buttonDoubeClick;
}
}
}
and just for completeness, since you said no third party dll's here's my SimpleDelegateCommand:
public class SimpleDelegateCommand : ICommand
{
private readonly Action _action;
public SimpleDelegateCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if(_action != null)
{
_action();
}
}
}
In a nutshell what's happening is this: when you assign the Command to the attached property, the OnButtonDoubleClickChanged event gets raised, at which point we hook up to the button.MouseDoubleClick event.
这篇关于我如何添加一个的ICommand从FrameworkElement的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!