2009-07-31 54 views
4

我有一個包含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。任何幫助,將不勝感激。谷歌搜索整晚,並沒有發現任何可用的東西在我的情況。請原諒我的英語。

回答

7

您已經爲您的依賴屬性註冊了錯誤的名稱。它應該是:

public static readonly DependencyProperty CreateItemCommandProperty = 
     DependencyProperty.Register(
      "CreateItemCommand", 
      typeof(ICommand), 
      typeof(MutableComboBox) 
     ); 

注意字符串是「CreateItemCommand」。您應該通讀this MSDN documentation以獲取有關依賴項屬性約定的詳細信息。

+0

謝謝!就是這樣。假設我需要詳細瞭解它是如何工作的。我唯一知道的是它必須在命名空間內是唯一的。那麼在DependencyProperty.Register()中名稱的標準是什麼?只需從聲明或別的東西的名稱中刪除單詞屬性? – dvn 2009-07-31 09:49:42