【发布时间】:2013-12-27 01:14:11
【问题描述】:
我正在为使用 WCF 服务从数据库接收数据的 Windows 商店开发应用程序(MVVM 模式)。 我想将一个类别列表数据绑定到组合框中,但它不适合我,我搜索了网络并没有找到解决方案。
班级类别:
public Category(Category c)
{
this.Id=c.Id;
this.Name = c.Name;
}
public int Id { get; set; }
public string Name { get; set; }
Xaml:
<ComboBox x:Name="ChooseCategory"
ItemsSource="{Binding ListCategories}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding SelectedItem, Mode=TwoWay}"/>
视图模型:
public ObservableCollection<Category> ListCategories { get; private set; }
在 OnNavigatedTo 函数中:
var listCategory = await proxy.GetAllCategoriesAsync();
List<Category> list = new List<Category>();
foreach (var item in listCategory)
{
list.Add(new Category(item));
}
ListCategories = new ObservableCollection<Category>(list);
有人吗???
【问题讨论】:
-
您是否调试过实际失败的原因?您实际上是否在 OnNavigatedTo 函数中获取值?输出部分是否有错误?您的 ViewModel 是否实现了 INotifyPropertyChanged?请提供更多信息。