问题描述
好的.我放弃.我想在我的Windows Phone应用程序之一中使用ListPicker控件.我收到异常 SelectedItem必须始终设置为有效值.
Ok. I give up.I want to use ListPicker control in one of my Windows Phone apps. I am getting an Exception SelectedItem must always be set to a valid value.
这是我的XAML ListPicker:
This is my XAML piece of ListPicker:
<toolkit:ListPicker x:Name="CategoryPicker"
FullModeItemTemplate="{StaticResource CategoryPickerFullModeItemTemplate}"
Margin="12,0,0,0"
ItemsSource="{Binding CategoryList}"
SelectedItem="{Binding SelectedCategory, Mode=TwoWay}"
ExpansionMode="ExpansionAllowed"
FullModeHeader="Pick Categories"
CacheMode="BitmapCache"
Width="420"
HorizontalAlignment="Left" />
CategoryList
是我的ViewModel中的ObservableCollection<Category>
.SelectedCategory
是我的ViewModel中类别类型的属性.
CategoryList
is an ObservableCollection<Category>
in my ViewModel.SelectedCategory
is a property in my ViewModel of type Category.
这就是我同时声明CategoryList和SelectedCategory的方式:
This is how I am declaring both CategoryList and SelectedCategory:
private Category _selectedCategory;// = new Category();
private ObservableCollection<Category> _categoryList = new ObservableCollection<Category>();
public ObservableCollection<Category> CategoryList
{
get
{
return _categoryList;
}
set
{
_categoryList = value;
RaisePropertyChanged("CategoryList");
}
}
public Category SelectedCategory
{
get
{
return _selectedCategory;
}
set
{
if (_selectedCategory == value)
{
return;
}
_selectedCategory = value;
RaisePropertyChanged("SelectedCategory");
}
}
感谢您的帮助!!!也许我不太了解ListPicker的用法.
Appreciate your help!!! Maybe I have not understood the usage of ListPicker very well.
推荐答案
看看我对这个问题的回答:带有值转换器的Silverlight ComboBox绑定
Take a look at my answer to this question:Silverlight ComboBox binding with value converter
简短的回答是,所选项目必须是集合中包含的项目.您的获取者正在将所选项目设置为新对象.此新对象未包含在集合中
The short answer is that the selected item must be an item that is contained within the collection. Your getter is setting the selected item to a new object. This new object is not contained within the collection
这篇关于使用ListPicker和DataBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!