代码之家  ›  专栏  ›  技术社区  ›  dvn

从用户控件外部绑定到命令

  •  4
  • dvn  · 技术社区  · 17 年前

    我有一个包含标签、组合框和按钮的简单用户控件。简而言之,它将在我的几乎所有视图中多次使用,每次使用绑定到我的ViewModel属性的不同itemssource和createItemCommand提供。

    标签和组合框是另一个用户控件(LabeledComboBox)的一部分,它工作正常。

    问题是,当我尝试在包含用户控件的窗口中绑定命令时,会得到以下异常:

    不能在“mutablecomboBox”类型的“createItemCommand”属性上设置“binding”。只能在DependencyObject的DependencyProperty上设置“binding”。

    以下是可变组合框的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
    }
    

    如您所见,我使用两种不同的方法来注册我的dps: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被设置为适当的ViewModel,它提供一个可观察的收件人集合和一个ICommand CreateNewRecipient作为简单属性。

    我做错什么了?在这种特殊情况下,我唯一想要的就是公开一个button.command属性,以便在用户控件之外使用,就像itemssource一样。我是否试图用错误的方式使用命令?如何从其他控件或窗口绑定到用户控件的命令?

    把我当作一个有这个命令和依赖属性的新手。任何帮助都将不胜感激。整晚都在谷歌上搜索,没有发现任何有用的东西。请原谅我的英语。

    1 回复  |  直到 14 年前
        1
  •  7
  •   Tim Cooper    14 年前

    您为依赖项属性注册了错误的名称。应该是:

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

    注意字符串是“createItemCommand”。你应该通读 this MSDN documentation 有关依赖属性约定的详细信息。

    推荐文章