问题描述
是否可以将多个命令绑定到按钮上.
Is it possible to bind the multiple commands to the button.
我有一个用户控件,我在主应用程序(父应用程序)中调用它.
I have a user control, which i am calling in my main application (parent application).
我想在两个控件(用户控件以及主窗口)上处理单击命令.但是我只能得到一个.
I want to handle a click command on both the controls (the user control as well as on the main window). However i am only able to get one.
有什么办法可以得到这个.
Is there any way in which i can get this.
非常感谢任何帮助.
代码片段:
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();
}
}
问候,图沙尔
推荐答案
您可能正在尝试聚合多个命令,这是很自然的事情.
You might be trying to aggregate multiple commands, which is a natural thing to want to do.
如果您使用的是 Prism,则有一个名为 CompositeCommand 的内置类(向下滚动一点):https://msdn.microsoft.com/en-us/library/ff921126.aspx
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
否则,Josh Smith 有一篇关于他的实现的非常好的文章,称为命令组":http://www.codeproject.com/KB/WPF/commandgroup.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.
这篇关于多命令绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!