WPF ComboBox display selected value?

这一生的挚爱 提交于 2020-01-07 08:16:04

问题


Before displaying the UI, data will be retrieved from database and these data to be displayed on screen.

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}"

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}"

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?

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


回答1:


Has your Color class ColorID property, or just Id?

If so: SelectedValuePath="Id"



来源:https://stackoverflow.com/questions/20629458/wpf-combobox-display-selected-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!