在我们的应用程序中,我们有一个非常大的数据集,它们充当 ComboBox 列表等的数据字典。这些数据是静态缓存的,并且由 2 个变量键控,所以我认为编写一个从 ComboBox 派生并公开的控件是明智的2个键作为DP。当这两个键具有正确的值时,我会自动从它对应的数据字典列表中设置 ComboBox 的 ItemsSource。我还自动将构造函数中的 SelectedValuePath 和 DisplayMemberPath 分别设置为 Code 和 Description。
以下是数据字典列表中 ItemsSource 中的项目的外观示例:
public class DataDictionaryItem
{
public string Code { get; set; }
public string Description { get; set; }
public string Code3 { get { return this.Code.Substring(0, 3); } }
}
Code的值总是4个字符长,但有时我只需要绑定3个字符。因此,Code3 属性。
下面是代码在我的自定义组合框中设置 ItemsSource 的方式:
private static void SetItemsSource(CustomComboBox combo)
{
if (string.IsNullOrEmpty(combo.Key1) || string.IsNullOrEmpty(combo.Key2))
{
combo.ItemsSource = null;
return;
}
List<DataDictionaryItem> list = GetDataDictionaryList(combo.Key1, combo.Key2);
combo.ItemsSource = list;
}
现在,我的问题是,当我将 XAML 中的 SelectedValuePath 更改为 Code3 时,它不起作用。我绑定到 SelectedValue 的内容仍然从 DataDictionaryItem 获得完整的 4 字符代码。我什至尝试在 SelectedValuePath 更改且没有骰子时重新运行 SetItemsSource。
谁能看到我需要做什么才能唤醒我的自定义组合框并使用提供的 SelectedValuePath(如果它在 XAML 中被覆盖)?在我的 SelectedValue 绑定业务对象中调整属性设置器中的值不是一个选项。
以下是 XAML 在表单中查找我的组合框的方式:
<c:CustomComboBox Key1="0" Key2="8099" SelectedValuePath="Code3" SelectedValue="{Binding Thing}"/>
编辑:我刚刚对我的代码运行了 snoop,它说我的 SelectedValuePath 是代码......它似乎从未设置为 Code3...... Zuh?