UserControlの外部からのコマンドへのバインド
-
06-07-2019 - |
質問
Label、ComboBox、およびButtonを含む単純なUserControlがあります。簡単に言えば、ViewModelプロパティへのバインディングを使用して、異なるItemsSourceとCreateItemCommandが提供されるたびに、ほとんどすべてのビューで何度も使用されることになります。
LabelとComboBoxは、正常に動作する別のUserControl(LabeledComboBox)の一部です。
問題は、ユーザーコントロールを含むウィンドウでコマンドをバインドしようとすると、次の例外が発生することです。
「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>
ここにコードビハインドがあります:
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は、RecipientsのObservableCollectionとICommand CreateNewRecipientを単純なプロパティとして提供する適切なViewModelに設定されます。
間違っているのは何ですか?この特定の場合に必要なのは、ItemsSourceのように、UserControlの外部で使用するButton.Commandプロパティを公開することだけです。コマンドを間違った方法で使用しようとしていますか?他のコントロールまたはウィンドウからUserControlsのコマンドにバインドするにはどうすればよいですか?
このコマンドとDependencyPropertyのものでnewbを考えてください。任意の助けをいただければ幸いです。一晩中Googleで検索しましたが、私の場合は何も使用できませんでした。私の英語をご容赦ください。
解決
依存関係プロパティに間違った名前を登録しました。次のようになります。
public static readonly DependencyProperty CreateItemCommandProperty =
DependencyProperty.Register(
"CreateItemCommand",
typeof(ICommand),
typeof(MutableComboBox)
);
文字列は&quot; CreateItemCommand&quot;であることに注意してください。規則の詳細については、このMSDNドキュメントをお読みください。依存関係プロパティ。