我试图在multiSelection模式下从lispicker中检索selectedItems。数据显示在FullModelItemTemplate中,但是当我选择项目时,我无法获取值。这是我的ListPicker XAML
<toolkit:ListPicker x:Name="location" ItemsSource="{Binding Items,Mode=TwoWay}" ItemTemplate="{StaticResource DataTemplate2}" FullModeItemTemplate="{StaticResource DataTemplate3}" HeaderTemplate="{StaticResource header}" SelectionMode="Multiple" SelectedItems="{Binding SelectedItem,Mode=TwoWay}" >
MainViewModel
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
public ObservableCollection<ItemViewModel> Items { get; private set; }
public MainViewModel()
{
Items = new ObservableCollection<ItemViewModel>();
LoadData();
}
public const string SelectedItemPropertyName = "SelectedItem";
private ObservableCollection<ItemViewModel> selectedItem;
public ObservableCollection<ItemViewModel> SelectedItem
{
get
{
return selectedItem;
}
set
{
if (value != selectedItem)
{
selectedItem = value;
NotifyPropertyChanged(SelectedItemPropertyName);
}
}
}
public void LoadData()
{
// Sample data; replace with real data
this.Items.Add(new ItemViewModel() { ID = "0", Cuisine = "Thai", CuisineImg = "Assets/Images/thsi.png", Location = "Shantinagar" });
this.Items.Add(new ItemViewModel() { ID = "1", Cuisine = "Indian", CuisineImg = "Assets/Images/indian.png", Location = "Mirpur" });
this.Items.Add(new ItemViewModel() { ID = "2", Cuisine = "Chinese", CuisineImg = "Assets/Images/chinese.png", Location = "Dhanmondi" });
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ItemViewModel
public class ItemViewModel : INotifyPropertyChanged
{
private string _id;
public string ID
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("ID");
}
}
}
private string _location;
public string Location
{
get
{
return _location;
}
set
{
if (value != _location)
{
_location = value;
NotifyPropertyChanged("Location");
}
}
}
private string _cuisine;
public string Cuisine
{
get
{
return _cuisine;
}
set
{
if (value != _cuisine)
{
_cuisine = value;
NotifyPropertyChanged("Cuisine");
}
}
}
private string _cuisineImg;
public string CuisineImg
{
get
{
return _cuisineImg;
}
set
{
if (value != _cuisineImg)
{
_cuisineImg = value;
NotifyPropertyChanged("CuisineImg");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
请注意,我正在使用MVVM灯,如果selectionMode是单一的,我在获取所选值时没有遇到任何问题,但是multiSelection模式导致了问题