这份原稿.WP7上ListPicker的微绑定约定
本文关键字:绑定 约定 ListPicker WP7 | 更新日期: 2023-09-27 18:02:50
我正在尝试口径。一个新项目的微框架,但我坚持绑定一个ListPicker(来自工具包)。当我将控件更改为简单的DropDown时,一切都如预期的那样工作。我假设DropDown工作正常,因为这里实现了默认约定:
AddElementConvention<Selector>(Selector.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
.ApplyBinding = (viewModelType, path, property, element, convention) => {
if (!SetBinding(viewModelType, path, property, element, convention))
return false;
ConfigureSelectedItem(element, Selector.SelectedItemProperty,viewModelType, path);
ApplyItemTemplate((ItemsControl)element, property);
return true;
};
ListPicker不实现选择器,所以我试着在我的引导程序中添加一个自定义约定:
static void AddCustomConventions() {
AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
.ApplyBinding = (viewModelType, path, property, element, convention) => {
ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty,viewModelType, path);
return true;
};
}
不幸的是,这不起作用。你能帮忙吗?
我用这个约定解决了问题。
ConventionManager.AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
.ApplyBinding = (viewModelType, path, property, element, convention) =>
{
if (ConventionManager.GetElementConvention(typeof(ItemsControl)).ApplyBinding(viewModelType, path, property, element, convention))
{
ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty, viewModelType, path);
return true;
}
return false;
};
此外,还有另一个问题。我的SelectedItem属性返回null,但是我的Items属性不包含空值。我得到一个异常,选择的项目是无效的,因为它不在列表中。