【发布时间】:2011-01-25 05:12:00
【问题描述】:
在我们的应用程序中,我们有一个非常大的数据集,作为 ComboBox 列表等的数据字典。这些数据被静态缓存并由 2 个变量键控,所以我认为编写一个从ComboBox 并将 2 个键暴露为 DP。当这 2 个键具有正确的值时,我会从它对应的数据字典列表中自动设置 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 是 Code...它似乎从未设置为 Code3...Zuh?
【问题讨论】:
-
是的,这是臭代码,但它也是示例代码,以免将我的整个应用程序粘贴在这里。
-
(顺便说一下,是 8 个字节和 6 个字节,而不是 4 个字节和 3 个字节。)
-
好的,我说的是字符而不是字节。
标签: wpf combobox itemssource