无法将命令传递/绑定到 WPF 用户控件
我正在尝试将命令传递给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Button
的默认DataContext
是 UserControl 的DataContext
,而不是 UserControl,因此您尝试绑定到DataContext.Command< /code> 而不是
UserControl.Command
要绑定到
UserControl.Command
,请使用RelativeSource
绑定EDIT 刚刚注意到<一href="https://stackoverflow.com/questions/7957123/cannot-pass-bind-command-to-wpf-user-control/7957233#7957233">HB的答案,这也可以。通常我更喜欢
RelativeSource
绑定而不是ElementName
绑定,因为有时我会重命名项目并习惯忘记其他控件通过名称引用该项目The default
DataContext
for yourButton
is your UserControl'sDataContext
, not your UserControl, so you are trying to bind toDataContext.Command
instead ofUserControl.Command
To bind to
UserControl.Command
, use aRelativeSource
bindingEDIT Just noticed HB's answer, which would also work. Usually I prefer
RelativeSource
bindings toElementName
ones because sometimes I rename items and used to forget what other controls reference that item by Name为控件命名并使用
ElementName
:Name the control and use
ElementName
:好吧,我只需绑定到来自主机控件/窗口的命令。因为数据上下文将从它继承。像这样的东西。
然后,您可以从代码隐藏文件中删除依赖属性代码。
并删除命令属性绑定,
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.
Then you can remove the dependency property code from the code-behind file.
And remove the command property binding,
一般来说,我总是遵循 Rachel 的建议(如上面的一个)。但在这种特殊情况下,我会考虑在用户控件的根元素上设置 DataContext,然后在其中进行整齐的绑定。
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.