WPF - 用户控件中的组合框的选定值不绑定

时间:2013-01-08 05:16:04

标签: wpf binding user-controls

我有可重用的用户控件,其中包含一个文本块和一个组合框,如下所示:

<UserControl ....>
        <TextBlock DockPanel.Dock="Left" x:Name="label">Title:/TextBlock>
        <ComboBox x:Name="comboBox" ></ComboBox>
</UserControl>

我在此控件的代码后面创建了以下依赖项属性。

public partial class ctlCombobox : UserControl
    {

    public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true }); 
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });          
    public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(ctlCombobox), new FrameworkPropertyMetadata(OnSelectedValueChanged) { BindsTwoWayByDefault = true });


    public ctlCombobox()

    {
        InitializeComponent();

        Binding selectedIndexBinding = new Binding("SelectedIndex") { Source = this, Mode = BindingMode.TwoWay };
        Binding itemsSourceItemBinding = new Binding("ItemsSource") { Source = this, Mode = BindingMode.TwoWay };
        Binding displayMemberPathBinding = new Binding("DisplayMemberPath") { Source = this, Mode = BindingMode.OneWay };
        Binding selectedItemBinding = new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay };
        Binding selectedValueBinding = new Binding("SelectedValue") { Source = this, Mode = BindingMode.TwoWay };
        Binding selectedValuePathBinding = new Binding("SelectedValuePath") { Source = this, Mode = BindingMode.TwoWay };

        comboBox.SetBinding(ComboBox.SelectedIndexProperty, selectedIndexBinding);
        comboBox.SetBinding(ComboBox.ItemsSourceProperty, itemsSourceItemBinding);
        comboBox.SetBinding(ComboBox.DisplayMemberPathProperty, displayMemberPathBinding);
        comboBox.SetBinding(ComboBox.SelectedItemProperty, selectedItemBinding);
        comboBox.SetBinding(ComboBox.SelectedValueProperty, selectedValueBinding);
        comboBox.SetBinding(ComboBox.SelectedValuePathProperty, selectedValuePathBinding);
    }


    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    [Browsable(true)]
    public int SelectedIndex
    {
        get { return (int)GetValue(SelectedIndexProperty); }
        set { SetValue(SelectedIndexProperty, value); }
    }

    [Browsable(true)]
    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    [Browsable(true)]
    public object SelectedValue
    {
        get { return (object)GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

    [Browsable(true)]
    public string SelectedValuePath
    {
        get { return (string)GetValue(SelectedValuePathProperty); }
        set { SetValue(SelectedValuePathProperty, value); }
    }

}

我在主控件中引用了上面的控件,如下所示:

<UserControl Name="mainControl" ...>   
    <DataGrid Name="lvEmployee" ItemsSource="{Binding Path=Employees, Mode=TwoWay}"  
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>                    
               <cc:ctlCombobox x:Name="cmbEmployeeType" 
                 ItemsSource="{Binding Source={x:Reference mainControl}, Path=DataContext.EmployeeTypes}"          
                 DisplayMemberPath="Name" SelectedValuePath="Id"   SelectedValue="{Binding   
                 Path=EmployeeTypeId}"  ">
               </cc:ctlCombobox>
         </DataTemplate>
        </DataGrid.RowDetailsTemplate>
  </DataGrid>

我正在使用MVVM模式。我正在尝试实现cmbEmployeeType以查找所有可能的员工类型。此外,当显示员工时,我希望cmbEmployeeType选择适当的员工类型。目前,在加载主控件时,会填充cmbEmployeeType组合框,但在组合框中未选中任何内容。我已将组合框绑定到Employee的员工类型ID。包含显示员工名字和姓氏的文本框的用户控件正常工作。输出中没有绑定错误。此外,当我使用普通的组合框而不是在用户控件中使用我的组合框时,没有问题。谢谢你的回复。

1 个答案:

答案 0 :(得分:2)

我在解决这个问题时遇到了问题,但可以找到解决方案here