title: Binding to Command from outside a UserControl
body: <p>I have a simple UserControl containing Label, ComboBox and Button. In brief, it is to be used throughout almost all of my Views many times, each time supplied with different ItemsSource and CreateItemCommand using Bindings to my ViewModel properties.</p>
  
  <p>The Label and ComboBox are part of another UserControl (LabeledComboBox) which works just fine.</p>
  
  <p>The problem is that when I try to bind a command in a window that contains my UserControl I get the following exception:</p>
  
  <blockquote>
    <p>A 'Binding' cannot be set on the 'CreateItemCommand' property of type 'MutableComboBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.</p>
  </blockquote>
  
  <p>Here is the XAML for MutableComboBox:</p>
  
  <pre><code>&lt;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" &gt;
  &lt;StackPanel Height="25" Orientation="Horizontal"&gt;
      &lt;uc:LabeledComboBox x:Name="ComboBoxControl"
          Label = "{Binding ElementName=MCB, Path=Label}"
          ItemsSource="{Binding ElementName=MCB, Path=ItemsSource}"
          SelectedItem="{Binding ElementName=MCB, Path=SelectedItem}" /&gt;
      &lt;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}"/&gt;
  &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Here is the codebehind for it:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>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 to use Button.CommandProperty.AddOwner(...) but had the same exception.</p>
  
  <p>Here is how I try to bind:</p>
  
  <pre><code>&lt;Window ...
      xmlns:uc="clr-namespace:Albo.Presentation.Templates"&gt;
      &lt;uc:MutableComboBox Label="Combo" 
          ItemsSource="{Binding Path=Recipients}"
          CreateItemCommand="{Binding Path=CreateNewRecipient}"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The DataContext of the window is set to appropriate ViewModel which provides an ObservableCollection of Recipients and an ICommand CreateNewRecipient as simple properties.</p>
  
  <p>What am I doing wrong? The only thing I want in this particular case is to expose a Button.Command property for use outside of my UserControl, just as the ItemsSource. Am I trying to use commands the wrong way? How can I bind to commands of my UserControls from other controls or windows?</p>
  
  <p>Consider me a newb with this Command and DependencyProperty stuff. Any help would be appreciated. Googled all night and didn't find anything usable in my case. Pardon my english.</p>