2009-07-31 25 views
4

私は、Label、ComboBox、およびButtonを含む簡単なUserControlを持っています。簡単に言えば、私のViewsのほとんどすべてで何回も使用され、毎回異なるItemsSourceおよびCreateItemCommandが、ViewModelプロパティにバインディングを使用して提供されます。UserControlの外部からのコマンドへのバインド

LabelとComboBoxは、うまく動作する別のUserControl(LabeledComboBox)の一部です。

問題は、私は私のユーザーコントロールを含むウィンドウにコマンドをバインドしようとすると、私は次の例外を取得:

A 'のタイプの「CreateItemCommand」プロパティに設定することができません「バインド」 MutableComboBox ' 「Binding」は、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 
} 

あなたが見ることができるように、私は私のDPを登録するには2つの異なるアプローチを使用しますの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を提供ViewModelにに適切に設定されている。ここで

は私がバインドしようとする方法です。

私は間違っていますか?私がこの特定のケースで欲しいのは、ItemsSourceと同じように、UserControlの外で使用するButton.Commandプロパティを公開することだけです。私は間違った方法でコマンドを使用しようとしていますか?他のコントロールやウィンドウからUserControlのコマンドにバインドする方法はありますか?

私はこのコマンドとDependencyPropertyのものでnewbを考えてください。どんな助けもありがとう。一晩中グーグルで探検し、私の場合には使えるものは何も見つかりませんでした。私の英語を赦してください。

答えて

7

依存関係プロパティに間違った名前を登録しました。それはする必要があります:

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

文字列は "CreateItemCommand"です。依存関係の規則の詳細については、this MSDN documentationを参照してください。

+0

ありがとうございました!それはそれだった。私はそれがどのように動作するかについてもっと読む必要があると仮定します。私が知っていた唯一のことは、名前空間内で一意でなければならないということでした。では、DependencyProperty.Register()の名前の基準は何ですか?宣言などの名前からPropertyという単語を削除するだけですか? – dvn

+1

問題ありません。あなたの理解に役立つ私の答えへのリンクを追加しました。 –

+0

リンクはもう利用できません。 – eYe

関連する問題