Hello.
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.
The Label and ComboBox are part of another UserControl (LabeledComboBox) which works just fine.
The problem is that when I try to bind a command in a window that contains my UserControl I get the following exception:
A 'Binding' cannot be set on the 'CreateItemCommand' property of type 'MutableComboBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Here is the XAML for 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 codebehind 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 to use Button.CommandProperty.AddOwner(...) but had the same exception.
Here is how I try to bind:
<Window ...
xmlns:uc="clr-namespace:Albo.Presentation.Templates">
<uc:MutableComboBox Label="Combo"
ItemsSource="{Binding Path=Recipients}"
CreateItemCommand="{Binding Path=CreateNewRecipient}"/>
</Window>
The DataContext of the window is set to appropriate ViewModel which provides an ObservableCollection of Recipients and an ICommand CreateNewRecipient as simple properties.
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?
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.