多命令绑定

发布于 2024-07-29 20:21:09 字数 1254 浏览 7 评论 0 原文

是否可以将多个命令绑定到按钮。

我有一个用户控件,我在主应用程序(父应用程序)中调用它。

我想处理两个控件(用户控件以及主窗口)上的单击命令。 然而我只能得到一个。

有什么方法可以让我得到这个。

非常感谢任何帮助。

代码片段:

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();
        }
}

问候, 图沙尔

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.

Any help is really appreciated.

Code Snippet:

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();
        }
}

Regards,
Tushar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2024-08-05 20:21:09

AFAIK WPF 不提供任何开箱即用的功能来支持各个级别的多个命令绑定,但您可以尝试以下操作:

void ClickCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    IInputElement parent = (IInputElement) LogicalTreeHelper.GetParent((DependencyObject)sender);
    MainWindowFooterCommands.BXClickMainWindowCommand.Execute(e.Parameter, parent);
}

不过,您可能必须测试您的父级是否确实是 IInputElement。

AFAIK WPF doesnt offer anything out of the box to support multiple commandbindings at various levels, but you could try the following:

void ClickCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    IInputElement parent = (IInputElement) LogicalTreeHelper.GetParent((DependencyObject)sender);
    MainWindowFooterCommands.BXClickMainWindowCommand.Execute(e.Parameter, parent);
}

You might have to test whether your parent really is an IInputElement, though.

橙味迷妹 2024-08-05 20:21:09

您可能会尝试聚合多个命令,这是很自然的事情。

如果您使用的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文