我按照http://mobileworld.appamundi.com/blogs/andywigley/archive/2012/02/02/how-to-databind-selecteditems-of-the-listpicker-and-recurringdayspicker.aspx的说明在多选ListPicker中绑定SelectedItems .
当我将ItemsSource绑定到ViewModel中创建的对象的ObservableCollection时,ListPicker可以正常工作 . 但是,当我将ItemsSource绑定到从WCF服务获取的ObservableCollection时,问题就开始了 . ListPicker正确显示所有项目,也可以在主屏幕上选择并显示为所选项目 . 但是,当我单击选择器再次选择时,ListPicker无法“勾选”模板上的选定项目 .
我可以重写从服务中获取的所有元素的列表,但我想知道是否有一种解决该问题的简洁方法?
项目信息:WP 7.0,WCF服务,EntityFramework,大量咖啡
实体类:
DataContract(IsReference=true)]
public class TypeOfDish
{
[DataMember]
public int TypeOfDishID { get; set; }
[DataMember]
public string NameToDisplay { get; set; }
}
WCF服务:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[ApplyDataContractResolver]
IEnumerable<TypeOfDish> GetDishTypes();
}
XAML:
<StackPanel x:Name="DishTypeGroup" Orientation="Vertical" Width="400">
<helpers:ListPickerExtended x:Name="TypeOfDishPicker" >
ItemsSource="{Binding DishTypeList}"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedDishTypes, Mode=TwoWay}"
Margin="8,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=NameToDisplay}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</helpers:ListPickerExtended>
</StackPanel>
ListPickerExtended.cs
public class ListPickerExtended : ListPicker
{
public new IList SelectedItems
{
get
{
return (IList)GetValue(SelectedItemsProperty);
}
set
{
base.SetValue(SelectedItemsProperty, value);
}
}
}
视图模型
public ObservableCollection<TypeOfDish> DishTypeList
{
get
{
//myModel.DichTypes is loaded with data from calling
//GetDishTypesAsync() on my service client
return myModel.DishTypes;
}
}
ObservableCollection<object> _selectedDishes = new ObservableCollection<object>();
public ObservableCollection<object> SelectedDishTypes
{
get { return _selectedDishes; }
set
{
if (_selectedDishes == value) { return; }
_selectedDishes = value;
RaisePropertyChanged("SelectedDishTypes");
}
}
1 回答
我刚刚找到解决方案 .
每次加载页面时我都会从服务中获取数据 . 从ListPicker中的完整模式模板返回时,重新加载主页面,使用新获取的数据重置DishTypesList属性 . 尽管仍然设置了ListPicker.SelectedItems,但新对象与其中的对象不匹配 .
我只需要将数据加载到View Model构造函数中,并且ListPicker开始正常工作 .