WPF UserControls - 在 UserControl 内的按钮上设置 .Command 属性

发布于 2024-08-31 15:18:34 字数 468 浏览 1 评论 0原文

我有一个包含按钮和其他一些控件的 UserControl:

<UserControl>
  <StackPanel>
     <Button x:Name="button" />
     ...
  </StackPanel>
</UserControl>

当我创建该控件的新实例时,我想获取按钮的 Command 属性:

<my:GreatUserControl TheButton.Command="{Binding SomeCommandHere}">
</my:GreatUserControl>

当然,“TheButton.Command”功能不起作用。

所以我的问题是:使用 XAML,如何设置用户控件内按钮的 .Command 属性?

I've got a UserControl that contains a button and some other controls:

<UserControl>
  <StackPanel>
     <Button x:Name="button" />
     ...
  </StackPanel>
</UserControl>

When I create a new instance of that control, I want to get at the Button's Command property:

<my:GreatUserControl TheButton.Command="{Binding SomeCommandHere}">
</my:GreatUserControl>

Of course, the "TheButton.Command" thing doesn't work.

So my question is: Using XAML, how can I set the .Command property of the button inside my user control?

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

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

发布评论

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

评论(1

韬韬不绝 2024-09-07 15:18:34

将依赖属性添加到 UserControl 并将按钮的 Command 属性绑定到该属性。

因此,在您的 GreatUserControl 中:

public ICommand SomeCommand
{
    get { return (ICommand)GetValue(SomeCommandProperty); }
    set { SetValue(SomeCommandProperty, value); }
}

public static readonly DependencyProperty SomeCommandProperty =
    DependencyProperty.Register("SomeCommand", typeof(ICommand), typeof(GreatUserControl), new UIPropertyMetadata(null));

在您的 GreatUserControl 的 XAML 中:

<UserControl 
    x:Class="Whatever.GreatUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="me"
    >
    <Button Command="{Binding SomeCommand,ElementName=me}">Click Me!</Button>
</UserControl>

因此,您的按钮绑定到 UserControl 本身的命令。现在您可以在父窗口中进行设置:

<my:GreatUserControl SomeCommand="{Binding SomeCommandHere}" />

Add a dependency property to your UserControl and bind the button's Command property to that.

So in your GreatUserControl:

public ICommand SomeCommand
{
    get { return (ICommand)GetValue(SomeCommandProperty); }
    set { SetValue(SomeCommandProperty, value); }
}

public static readonly DependencyProperty SomeCommandProperty =
    DependencyProperty.Register("SomeCommand", typeof(ICommand), typeof(GreatUserControl), new UIPropertyMetadata(null));

And in your GreatUserControl's XAML:

<UserControl 
    x:Class="Whatever.GreatUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="me"
    >
    <Button Command="{Binding SomeCommand,ElementName=me}">Click Me!</Button>
</UserControl>

So your button binds to the command on the UserControl itself. Now you can set that in your parent window:

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