【发布时间】:2011-10-31 17:15:02
【问题描述】:
我正在尝试将命令传递给 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 属性为空。
将命令绑定到用户控件外部的按钮确实有效。
这里出了什么问题?
【问题讨论】:
标签: wpf binding user-controls command dependency-properties