我有可重用的用户控件,其中包含一个文本块和一个组合框,如下所示:
<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 组合框,但在组合框中没有选择任何内容。我已将组合框绑定到员工的员工类型 ID。包含显示员工名字和姓氏的文本框的用户控件工作正常。输出中没有绑定错误。此外,当我使用普通组合框而不是在用户控件中使用我的组合框时,也没有问题。感谢您的回复。