WPF ComboBox显示所选值? [英] WPF ComboBox display selected value?
问题描述
在显示UI之前,将从数据库中检索数据,并将这些数据显示在屏幕上.
Before displaying the UI, data will be retrieved from database and these data to be displayed on screen.
现在我有一个包含2个字段的屏幕.一个文本框
和一个 combobox
.
Now I have a screen with 2 fields. One textbox
and one combobox
.
文本框
的值被绑定,如下所示:
Value of textbox
being bind as below:
<TextBox Text="{Binding [someViewModel].person.name, Mode=TwoWay,
ValidatesOnDataErrors=True, ValidatesOnExceptions=True,
ValidatesOnNotifyDataErrors=True}"
现在,我有一个ComboBox,它可以从数据库中获取值列表,黄色,绿色,蓝色以下是我绑定 ComboBox
的方法:
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 IsSynchronizedWithCurrentItem="True"
ItemSource="{Binding [someViewModel].ColorList}"
DisplayMemberPath="ColorName" SelectedValuePath="ColorID"
SelectedValue="{Binding [someViewModel].person.ColorID}"
现在的问题是,当我加载页面时,默认选择的值将基于 [someViewModel] .person.ColorID
,但是显示为空.ComboBox中有值.当我调试它时, [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. There are values in the 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
推荐答案
您的Color类是否具有ColorID属性,或者只是ID?
Has your Color class ColorID property, or just Id?
如果是这样: SelectedValuePath ="Id"
这篇关于WPF ComboBox显示所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!