我有一个简单的UserControl包含Label,ComboBox和Button.简而言之,它几乎可以在我的几乎所有视图中使用,每次使用不同的ItemsSource和CreateItemCommand使用Bindings到我的ViewModel属性.
Label和ComboBox是另一个UserControl(LabeledComboBox)的一部分,它工作正常.
问题是,当我尝试在包含我的UserControl的窗口中绑定命令时,我得到以下异常:
无法在'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>
Run Code Online (Sandbox Code Playgroud)
这是它的代码隐藏:
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 …Run Code Online (Sandbox Code Playgroud)