我試圖傳遞一個命令到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
屬性爲空。
將命令綁定到用戶控件外部的按鈕確實有效。
這裏怎麼回事?
請查看Rachel的崗位 – David