无法将命令传递/绑定到 WPF 用户控件

发布于 2024-12-13 05:40:15 字数 1127 浏览 0 评论 0原文

我正在尝试将命令传递给 WPF 用户控件中的元素。

<UserControl x:Class="MyApp.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- shortened -->
    <Button Command="{Binding Command}">
        <Button.Content>
            <!-- shortened -->
        </Button.Content>
    </Button>
</UserControl>
public partial class MyControl : UserControl
{
   public static readonly DependencyProperty CommandProperty
      = DependencyProperty.Register("Command", 
                                           typeof(ICommand), typeof(MyControl));
   //shortened        
   public ICommand Command
   {
      get { return (ICommand)GetValue(CommandProperty); }
      set { SetValue(CommandProperty, value); }
   }
   //shortened
} 
<uc:MyControl Command="{Binding DoStuffCommand}" />  <!-- shortened -->

单击用户控件中的按钮时,没有任何反应。
当我调试时,Command 属性为 null。
将命令绑定到用户控件外部的按钮确实有效。
这里出了什么问题?

I'm trying to pass a command to an element in a WPF user control.

<UserControl x:Class="MyApp.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- shortened -->
    <Button Command="{Binding Command}">
        <Button.Content>
            <!-- shortened -->
        </Button.Content>
    </Button>
</UserControl>
public partial class MyControl : UserControl
{
   public static readonly DependencyProperty CommandProperty
      = DependencyProperty.Register("Command", 
                                           typeof(ICommand), typeof(MyControl));
   //shortened        
   public ICommand Command
   {
      get { return (ICommand)GetValue(CommandProperty); }
      set { SetValue(CommandProperty, value); }
   }
   //shortened
} 
<uc:MyControl Command="{Binding DoStuffCommand}" />  <!-- shortened -->

When the button in the user control is clicked, nothing happens.
When I debug, the Command property is null.
Binding the command to a button outside of the user control does work.
What's going wrong here?

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

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

发布评论

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

评论(4

独留℉清风醉 2024-12-20 05:40:15

Button 的默认 DataContext 是 UserControl 的 DataContext,而不是 UserControl,因此您尝试绑定到 DataContext.Command< /code> 而不是 UserControl.Command

要绑定到 UserControl.Command,请使用 RelativeSource 绑定

<Button Command="{Binding Command, RelativeSource={
        RelativeSource AncestorType={x:Type local:MyControl}}}">  

EDIT 刚刚注意到<一href="https://stackoverflow.com/questions/7957123/cannot-pass-bind-command-to-wpf-user-control/7957233#7957233">HB的答案,这也可以。通常我更喜欢 RelativeSource 绑定而不是 ElementName 绑定,因为有时我会重命名项目并习惯忘记其他控件通过名称引用该项目

The default DataContext for your Button is your UserControl's DataContext, not your UserControl, so you are trying to bind to DataContext.Command instead of UserControl.Command

To bind to UserControl.Command, use a RelativeSource binding

<Button Command="{Binding Command, RelativeSource={
        RelativeSource AncestorType={x:Type local:MyControl}}}">  

EDIT Just noticed HB's answer, which would also work. Usually I prefer RelativeSource bindings to ElementName ones because sometimes I rename items and used to forget what other controls reference that item by Name

相思故 2024-12-20 05:40:15

为控件命名并使用 ElementName

<UserControl ...
             Name="control">
    <Button Command="{Binding Command, ElementName=control}">
    <!-- ... -->

Name the control and use ElementName:

<UserControl ...
             Name="control">
    <Button Command="{Binding Command, ElementName=control}">
    <!-- ... -->
刘备忘录 2024-12-20 05:40:15

好吧,我只需绑定到来自主机控件/窗口的命令。因为数据上下文将从它继承。像这样的东西。

<UserControl x:Class="MyApp.MyControl" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <!-- shortened --> 
    <Button Command="{Binding DoStuffCommand}"> 
        <Button.Content> 
            <!-- shortened --> 
        </Button.Content> 
    </Button> 
</UserControl>

然后,您可以从代码隐藏文件中删除依赖属性代码。

并删除命令属性绑定,

<uc:MyControl />

Well, I would just bind to the command from the host control/window. Because the data context will be inherited from it. Something like this.

<UserControl x:Class="MyApp.MyControl" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <!-- shortened --> 
    <Button Command="{Binding DoStuffCommand}"> 
        <Button.Content> 
            <!-- shortened --> 
        </Button.Content> 
    </Button> 
</UserControl>

Then you can remove the dependency property code from the code-behind file.

And remove the command property binding,

<uc:MyControl />
不气馁 2024-12-20 05:40:15

一般来说,我总是遵循 Rachel 的建议(如上面的一个)。但在这种特殊情况下,我会考虑在用户控件的根元素上设置 DataContext,然后在其中进行整齐的绑定。

<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor,
                            AncestorType={x:Type UserControl}}}">
<!-- shortened -->
    <Button Command="{Binding Command}">
        <Button.Content>
            <!-- shortened -->
        </Button.Content>
    </Button>
</Grid >

In general, I always follow Rachel advice (like the one above). But in this particular case, I'd consider setting DataContext on the root element of the user control, then it'd make a neat binding in there.

<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor,
                            AncestorType={x:Type UserControl}}}">
<!-- shortened -->
    <Button Command="{Binding Command}">
        <Button.Content>
            <!-- shortened -->
        </Button.Content>
    </Button>
</Grid >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文