dvn*_*dvn 4 wpf user-controls command button
我有一个简单的UserControl包含Label,ComboBox和Button.简而言之,它几乎可以在我的几乎所有视图中使用,每次使用不同的ItemsSource和CreateItemCommand使用Bindings到我的ViewModel属性.
Label和ComboBox是另一个UserControl(LabeledComboBox)的一部分,它工作正常.
问题是,当我尝试在包含我的UserControl的窗口中绑定命令时,我得到以下异常:
无法在'MutableComboBox'类型的'CreateItemCommand'属性上设置'绑定'.'绑定'只能在DependencyObject的DependencyProperty上设置.
这是MutableComboBox的XAML:
<UserControl x:Class="Albo.Presentation.Templates.MutableComboBox"
x:Name="MCB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:Albo.Presentation.Templates" >
<StackPanel Height="25" Orientation="Horizontal">
<uc:LabeledComboBox x:Name="ComboBoxControl"
Label = "{Binding ElementName=MCB, Path=Label}"
ItemsSource="{Binding ElementName=MCB, Path=ItemsSource}"
SelectedItem="{Binding ElementName=MCB, Path=SelectedItem}" />
<Button x:Name="CreateItemButton"
Grid.Column="1" Width="25" Margin="2,0,0,0"
Content="+" FontFamily="Courier" FontSize="18"
VerticalContentAlignment="Center"
Command="{Binding ElementName=MCB, Path=CreateItemCommand}"/>
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
这是它的代码隐藏:
public partial class MutableComboBox : UserControl
{
public MutableComboBox()
{
InitializeComponent();
}
public string Label
{
get { return this.ComboBoxControl.Label; }
set { this.ComboBoxControl.Label = value; }
}
#region ItemsSource dependency property
public static readonly DependencyProperty ItemsSourceProperty =
ItemsControl.ItemsSourceProperty.AddOwner(
typeof(MutableComboBox),
new PropertyMetadata(MutableComboBox.ItemsSourcePropertyChangedCallback)
);
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static void ItemsSourcePropertyChangedCallback(
DependencyObject controlInstance,
DependencyPropertyChangedEventArgs e)
{
MutableComboBox myInstance = (MutableComboBox)controlInstance;
myInstance.ComboBoxControl.ItemsSource = (IEnumerable)e.NewValue;
}
#endregion // ItemsSource dependency property
#region SelectedItem dependency property
// It has just the same logic as ItemsSource DP.
#endregion SelectedItem dependency property
#region CreateItemCommand dependency property
public static readonly DependencyProperty CreateItemCommandProperty =
DependencyProperty.Register(
"MutableComboBoxCreateItemCommandProperty",
typeof(ICommand),
typeof(MutableComboBox)
);
public ICommand CreateItemCommand
{
get { return (ICommand)GetValue(CreateItemCommandProperty); }
set { SetValue(CreateItemCommandProperty,value); }
}
#endregion // CreateItem dependency property
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用两种不同的方法来注册我的DP:ItemsSource来自ItemsControl DP,CreateItemCommand是由DependencyProperty.Register(...)创建的.我尝试使用Button.CommandProperty.AddOwner(...)但有相同的异常.
这是我尝试绑定的方式:
<Window ...
xmlns:uc="clr-namespace:Albo.Presentation.Templates">
<uc:MutableComboBox Label="Combo"
ItemsSource="{Binding Path=Recipients}"
CreateItemCommand="{Binding Path=CreateNewRecipient}"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
窗口的DataContext设置为适当的ViewModel,它提供了一个ObservableCollection的Recipients和一个ICommand CreateNewRecipient作为简单属性.
我究竟做错了什么?在这种特殊情况下,我唯一想要的是公开一个Button.Command属性,以便在我的UserControl之外使用,就像ItemsSource一样.我试图以错误的方式使用命令吗?如何从其他控件或窗口绑定到UserControls的命令?
考虑一下这个Command和DependencyProperty的新东西.任何帮助,将不胜感激.用了一整天的Google搜索,在我的案例中找不到任何可用的东西.原谅我的英语.
您已为依赖项属性注册了错误的名称.它应该是:
public static readonly DependencyProperty CreateItemCommandProperty =
DependencyProperty.Register(
"CreateItemCommand",
typeof(ICommand),
typeof(MutableComboBox)
);
Run Code Online (Sandbox Code Playgroud)
请注意该字符串是"CreateItemCommand".您应该阅读此MSDN文档,以获取有关依赖项属性约定的详细信息.