在 WPF 用户控件中绑定到路由事件的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15082045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command Binding to Routed Event in WPF User-control
提问by Hardik
I want to bind Viewmodel command to Usercontrol's Routed Event. Here is the detailed explanation of what I have.
我想将 Viewmodel 命令绑定到用户控件的路由事件。这是我所拥有的详细解释。
I have a User Control which have one Image(which shows image) and one Buttonat bottom (Buttonto remove Image). I am using a Usercontrol in a ListView.
我有一个用户控件,它有一个Image(显示图像)和一个Button在底部(Button删除Image)。我在ListView.
In my Usercontrol's Code behind I have a RoutedEventHandlerto remove the Image:
在我的用户控件后面的代码中,我有一个RoutedEventHandler删除Image:
public event RoutedEventHandler RemoveImage;
In the window where I use this Usercontrol, I have put:
在我使用这个用户控件的窗口中,我放置了:
<uc:ucImageListItem x:Name="ImageListItem" RemoveImage="ImageListItem_RemoveImage" />
This code works correctly if My code to remove image is in code behind. but I want to Bind command of Viewmodel to RemoveImage RoutedEvent.
如果我的删除图像的代码在后面的代码中,则此代码可以正常工作。但我想将 Viewmodel 的命令绑定到 RemoveImage RoutedEvent。
Probably like (not correct)
大概喜欢(不正确)
<uc:ucImageListItem x:Name="ImageListItem" RemoveImage="{binding CommandtoRemove}" />
How to achieve this?
如何实现这一目标?
I found something related to RoutedCommandor DependancyProperty, but could not find any proper way, How to use them.
我找到了与RoutedCommandor相关的东西DependancyProperty,但找不到任何正确的方法,如何使用它们。
Let me know if I need to further clear my question. Thanks in anticipation.
如果我需要进一步澄清我的问题,请告诉我。感谢期待。
回答by yo chauhan
Hi this piece of code shows how to call command: Command handler
嗨,这段代码显示了如何调用命令: 命令处理程序
public class CommandHandler : ICommand
{
public CommandHandler(Action<object> action,Func<object,bool> canexecute)
{
_action = action;
_canExecute = canexecute;
}
Action<object> _action;
Func<object, bool> _canExecute;
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action(parameter);
}
}
ViewModel
视图模型
public class MainViewModel
{
private CommandHandler _buttonCommand;
public CommandHandler ButtonCommand
{
get
{
return _buttonCommand ?? (_buttonCommand = new CommandHandler((param) => OnButtonCommand(param),(param)=>true));
}
}
private void OnButtonCommand(object obj)
{
//DO things here whatever you want to do on Button click
}
}
View
看法
<Button Command="{Binding ButtonCommand}" Content="ok"/>
you need to pass two parameters to CommandHandler Constructor one is Action that you want to fire on Command and second param is func that must return bool. If func evaluates to true only then the Action of Command is fired.And the param in action and func is what you will bind to the CommandParameter in my case above it will be null as I havent binded the CommandParameter.I hope this will help.
您需要将两个参数传递给 CommandHandler 构造函数,一个是要在 Command 上触发的 Action,第二个参数是必须返回 bool 的 func。如果 func 评估为真,则命令的动作被触发。并且在动作和 func 中的参数是你将绑定到 CommandParameter 在我上面的例子中它将为空,因为我没有绑定 CommandParameter。我希望这会有所帮助。

