从UserControl外部绑定到命令
我有一个包含Label,ComboBox和Button的简单UserControl。简而言之,它几乎可以在我的所有视图中使用很多次,每次都使用不同的ItemsSource和CreateItemCommand,使用绑定到我的ViewModel属性。从UserControl外部绑定到命令
标签和组合框是另一个UserControl(LabeledComboBox)的一部分,它工作得很好。
的问题是,当我尝试在包含我的用户窗口绑定命令我得到以下异常:
A“绑定”不能在类型的“CreateItemCommand”属性设置“ MutableComboBox”。 '绑定'只能在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>
这里是它的代码隐藏:
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
}
正如你所看到的,我用两种不同的方法来注册我的DPS:的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>
窗口的DataContext设置到适当的视图模型,其提供的收件人一个ObservableCollection和一个ICommand CreateNewRecipient简单属性。
我在做什么错?在这种特殊情况下,我唯一需要的是公开一个Button.Command属性,以便在我的UserControl之外使用,就像ItemsSource一样。我是否试图以错误的方式使用命令?我怎样才能绑定到我的UserControls从其他控件或窗口的命令?
考虑我与这个命令和DependencyProperty东西newb。任何帮助,将不胜感激。谷歌搜索整晚,并没有发现任何可用的东西在我的情况。请原谅我的英语。
您已经为您的依赖属性注册了错误的名称。它应该是:
public static readonly DependencyProperty CreateItemCommandProperty =
DependencyProperty.Register(
"CreateItemCommand",
typeof(ICommand),
typeof(MutableComboBox)
);
注意字符串是“CreateItemCommand”。您应该通读this MSDN documentation以获取有关依赖项属性约定的详细信息。
谢谢!就是这样。假设我需要详细了解它是如何工作的。我唯一知道的是它必须在命名空间内是唯一的。那么在DependencyProperty.Register()中名称的标准是什么?只需从声明或别的东西的名称中删除单词属性? – dvn 2009-07-31 09:49:42