多命令绑定
是否可以将多个命令绑定到按钮。
我有一个用户控件,我在主应用程序(父应用程序)中调用它。
我想处理两个控件(用户控件以及主窗口)上的单击命令。 然而我只能得到一个。
有什么方法可以让我得到这个。
非常感谢任何帮助。
代码片段:
public class MainWindowFooterCommands
{
public static readonly RoutedUICommand FooterClickLocalCommand = new RoutedUICommand("Local Button Command", "FooterClickLocalCommand", typeof(MainWindowFooterCommands));
}
private void MainWindowFooterBindCommands()
{
CommandBinding cmdBindingBXClick = new CommandBinding(MainWindowFooterCommands.FooterClickLocalCommand);
cmdBindingBXClick.Executed += ClickCommandHandler;
CommandBindings.Add(cmdBindingBXClick);
}
void ClickCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
//Do Something
}
//Parent Control holding an instance of the footer control.
class MainWindow {
public MainWindow()
{
CommandBinding cmdBindingBXClick1 = new CommandBinding(MainWindowFooterCommands.BXClickMainWindowCommand);
cmdBindingBXClick1.Executed += LoadParent;
CommandBindings.Add(cmdBindingBXClick1);
}
public void LoadParent(object sender, ExecutedRoutedEventArgs e)
{
LoadParentWindow();
}
}
问候, 图沙尔
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK WPF 不提供任何开箱即用的功能来支持各个级别的多个命令绑定,但您可以尝试以下操作:
不过,您可能必须测试您的父级是否确实是 IInputElement。
AFAIK WPF doesnt offer anything out of the box to support multiple commandbindings at various levels, but you could try the following:
You might have to test whether your parent really is an IInputElement, though.
您可能会尝试聚合多个命令,这是很自然的事情。
如果您使用的是 Prism,则有一个名为 CompositeCommand 的内置类(向下滚动一点):https://msdn.microsoft.com/en-us/library/ff921126.aspx
另外,Josh Smith 有一篇关于他的实现的非常好的文章,称为“命令组”:http://www.codeproject.com/KB/WPF/commandgroup.aspx
有您可以像这样汇总一些非常好的场景(例如,“全部保存”)。 一个适合你的技巧的好工具。
You might be trying to aggregate multiple commands, which is a natural thing to want to do.
If you are using Prism, there is a class builtin for this called the CompositeCommand (scroll down a bit): https://msdn.microsoft.com/en-us/library/ff921126.aspx
Otherwise, Josh Smith has a very good article on his implementation called a "Command Group": http://www.codeproject.com/KB/WPF/commandgroup.aspx
There are some very nice scenarios you can rollup like this (for instance, "Save All"). A good tool for your bag of tricks.