我在WP7中构建一个简单的粗糙表单存在问题。我花了很多时间将Enum显示到列表选择器中,现在我在尝试绑定到(IsolatedStorage)对象时看到InvalidCastException。Windows Phone 7 ListPicker InvalidCastException
public class Bath {
public string Colour { get; set; }
public WaterType WaterType { get; set; }
}
public enum WaterType {
Hot,
Cold
}
枚举绑定到ListPicker,但因为没有enum.GetValues()在WP7这不是一个简单的任务。
我有一个简单类型的类...
public class TypeList
{
public string Name { get; set; }
}
而在我的视图模型,我的ObservableCollection和枚举嘲笑值...
private ObservableCollection<TypeList> _WaterTypeList;
public ObservableCollection<TypeList> WaterTypeList
{
get { return _WaterTypeList; }
set
{
_WaterTypeList= value;
NotifyPropertyChanged("WaterTypeList");
}
}
public void LoadCollectionsFromDatabase()
{
ObservableCollection<TypeList> wTypeList = new ObservableCollection<WaterTypeList>();
wTypeList.Add(new TypeList{ Name = WaterType.Hot.ToString() });
wTypeList.Add(new TypeList{ Name = WaterType.Income.ToString() });
WaterTypeList = new ObservableCollection<TypeList>(wTypeList);
}
最后,我的XAML包含列表框...
<toolkit:ListPicker
x:Name="BathTypeListPicker"
ItemsSource="{Binding WaterTypeList}"
DisplayMemberPath="Name">
</toolkit:ListPicker>
我不确定上述是否是最佳做法,事实上如果上述是第问题,但上述确实给了我一个填充ListPicker。
最后,当提交表单时,cast会导致InvalidCastException。
private void SaveAppBarButton_Click(object sender, EventArgs e)
{
var xyz = WaterTypeList.SelectedItem; // type AppName.Model.typeList
Bath b = new Bath
{
Colour = ColourTextBox.Text ?? "Black",
WaterType = (WaterType)WaterTypeListPicker.SelectedItem
};
App.ViewModel.EditBath(b);
NavigationService.Navigate(new Uri("/Somewhere.xaml", UriKind.Relative));
}
}
有没有人遇到类似的问题,并可以提供建议。我看到我的opions将集中精力从ListPicker转换有意义的东西,还是应该重新思考ListPicker的填充方式?
那真棒,真该感谢,因为我是很好,真正卡住! – Gavin 2012-07-17 16:04:22