简体   繁体   中英

WPF ComboBox display selected value?

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

Has your Color class ColorID property, or just Id?

If so: SelectedValuePath="Id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM