【发布时间】:2018-03-26 16:35:08
【问题描述】:
我正在使用 uwp 和 mvvm-light 在 c# 中编写一个数据库管理应用程序,如果不先打开它并先手动选择一个,我无法在我的组合框中显示默认的 selectedValue。
这是我的看法:
<ComboBox x:Name="editCategory" Header="Category" ItemsSource="{Binding Categories}" SelectedValue="{Binding CategoryCode, Mode=TwoWay}" SelectedValuePath="Code" DisplayMemberPath="Name"/>
这是我的视图模型:
private ObservableCollection<Category> _categories;
public ObservableCollection<Category> Categories {
get { return _categories; }
set {
if (_categories == value)
return;
_categories = value;
RaisePropertyChanged("Categories");
}
}
private int _categoryCode;
public int CategoryCode {
get { return _categoryCode; }
set {
if (_categoryCode == value)
return;
_categoryCode = value;
RaisePropertyChanged("CategoryCode");
}
}
还有我的模特:
class Category
{
public int Code { get; set; }
[Required]
public string Name { get; set; }
}
我可以在打开组合框时显示不同的值,当我选择一个值时,它会在组合框关闭时正确显示。
我知道绑定正在工作,因为如果我在 ViewModel 的 CategoryCode 设置器中设置断点,它会显示正确的更新值。
问题是当我加载页面时,当它应该显示Category.Code = CategoryCode的项目的Category.Name时没有选择默认值
如果可以的话,请帮助我,我已经搜索了几个小时,但到目前为止我找不到任何帮助我
【问题讨论】:
标签: c# combobox mvvm-light uwp-xaml selectedvalue