[英]WPF ComboBox display selected value?
Before displaying the UI, data will be retrieved from database and these data to be displayed on screen. 在显示UI之前,将从数据库中检索数据,并将这些数据显示在屏幕上。
Now I have a screen with 2 fields. 现在,我有一个包含2个字段的屏幕。 One
textbox
and one combobox
. 一个
textbox
和一个combobox
。
Value of textbox
being bind as below: 绑定
textbox
值如下:
<TextBox Text="{Binding [someViewModel].person.name, Mode=TwoWay,
ValidatesOnDataErrors=True, ValidatesOnExceptions=True,
ValidatesOnNotifyDataErrors=True}"
Now I have a ComboBox that get the list of value from database, says Yellow, Green, Blue Below is the way I bind ComboBox
: 黄色,绿色,蓝色说,现在我有一个ComboBox可以从数据库中获取值列表。下面是绑定
ComboBox
:
<ComboBox IsSynchronizedWithCurrentItem="True"
ItemSource="{Binding [someViewModel].ColorList}"
DisplayMemberPath="ColorName" SelectedValuePath="ColorID"
SelectedValue="{Binding [someViewModel].person.ColorID}"
My problem now is when I load the page, the default selected value will be based on [someViewModel].person.ColorID
but it's showing empty instead. 我现在的问题是,当我加载页面时,默认选择的值将基于
[someViewModel].person.ColorID
但是显示为空。 There are values in the ComboBox. ComboBox中有值。 When I debug it, there are value in
[someViewModel].person.ColorID
but it just wouldn't show? 当我调试它时,在
[someViewModel].person.ColorID
有值,但是它不会显示?
When I change the value from Yellow to Green , the value will be reflected to [someViewModel].person.ColorID
as well which fits what I need 当我将值从Yellow更改为Green时 ,该值也会反映到
[someViewModel].person.ColorID
,这也符合我的需要
Has your Color class ColorID property, or just Id? 有您的Color类ColorID属性,还是只有ID?
If so: SelectedValuePath="Id"
如果是这样:
SelectedValuePath="Id"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.