Binding to a command from outside UserControl

I have a simple UserControl containing Label, ComboBox and Button. In short, it should be used in almost all of my views over and over again, each time coming with different ItemsSource and CreateItemCommand using Bindings for my ViewModel properties.

The Shortcut and ComboBox are part of another UserControl (LabeledComboBox) that works great.

The problem is that when I try to link a command in a window containing my UserControl, I get the following exception:

Binding cannot be set in the CreateItemCommand property of the MutableComboBox type. Binding can only be set in the DependencyProperty of a DependencyObject.

Here is the XAML for the MutableComboBox:

<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> 

Here is the code for it:

 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 } 

As you can see, I use two different approaches to registering my DPs: ItemsSource is taken from ItemsControl DP and CreateItemCommand is created by DependencyProperty.Register (...). I tried using Button.CommandProperty.AddOwner (...) but had the same exception.

This is how I try to link:

 <Window ... xmlns:uc="clr-namespace:Albo.Presentation.Templates"> <uc:MutableComboBox Label="Combo" ItemsSource="{Binding Path=Recipients}" CreateItemCommand="{Binding Path=CreateNewRecipient}"/> </Window> 

The corresponding ViewModel is set in the DataContext window of the window, which provides the ObservableCollection of recipients and the ICommand CreateNewRecipient as simple properties.

What am I doing wrong? The only thing I want in this particular case is to open the Button.Command property for use outside of my UserControl, as well as the ItemsSource. Am I trying to use the commands incorrectly? How can I bind my UserControls commands to other controls or windows?

Keep me fresh with this team and DependencyProperty stuff. Any help would be greatly appreciated. Google all night and did not find anything useful in my case. Sorry for my English.

+4
source share
1 answer

You have registered the wrong name for your dependency property. It should be:

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

Notice the line is "CreateItemCommand". You should read this MSDN documentation for more information on conventions for dependency properties.

+7
source

All Articles