WPF 用户控制 XAML 命令

发布于 2024-11-03 22:59:01 字数 538 浏览 3 评论 0原文

我有一个带有两个按钮的 3.5 WPF 用户控件,我希望能够通过用户控件的 xaml 将命令绑定到这些按钮。

x:Usercontrol x:Name:fou Button1Command="{Binding  StuffCommandHandler}" Button2Command="{Binding  Stuff2CommandHandler}" 

问题是上述绑定不起作用。 如何通过 xaml 将命令绑定到用户控件的按钮(其中两个)?

我在 UserControl 的代码后面得到了这个,并将 Button1CommandHandler 绑定到 Button1.Command

   private ICommand _button1Command;
   public ICommand Button1CommandHandler
    {
        get { return _button1Command; }
        set { _button1Command = value; }
    }

I've got a 3.5 WPF User Control with two buttons and I'd like to be able to bind commands to these buttons via the User Control's xaml.

x:Usercontrol x:Name:fou Button1Command="{Binding  StuffCommandHandler}" Button2Command="{Binding  Stuff2CommandHandler}" 

Problem is the above bindings are not working.
How can I bind commands to the User control's buttons, two of them, via xaml?

I've got this in the UserControl's code behind and I bind Button1CommandHandler to Button1.Command

   private ICommand _button1Command;
   public ICommand Button1CommandHandler
    {
        get { return _button1Command; }
        set { _button1Command = value; }
    }

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

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

发布评论

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

评论(1

很酷又爱笑 2024-11-10 22:59:01

您必须将 Button1CommandHandler 设为依赖属性:

public static readonly DependencyProperty ButtonCommandProperty =
    DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(TwoButtons), new PropertyMetadata(default(ICommand)));

public ICommand ButtonCommand
{
    get { return (ICommand)GetValue(ButtonCommandProperty); }
    set { SetValue(ButtonCommandProperty, value); }
}

然后将按钮的 Command 绑定到它。如果您从代码创建按钮,则可以像这样绑定它:

button.SetBinding(Button.CommandProperty, new Binding("ButtonCommand") { Source = this });

You have to make Button1CommandHandler into a dependency property:

public static readonly DependencyProperty ButtonCommandProperty =
    DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(TwoButtons), new PropertyMetadata(default(ICommand)));

public ICommand ButtonCommand
{
    get { return (ICommand)GetValue(ButtonCommandProperty); }
    set { SetValue(ButtonCommandProperty, value); }
}

and then bind your button's Command to it. If you create the button from code, you can bind it like this:

button.SetBinding(Button.CommandProperty, new Binding("ButtonCommand") { Source = this });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文