0

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

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
  • 1
    The first part (TextBox) of your question is unrelated to the problem and just makes your question longer to read and understand. – franssu Dec 17 '13 at 09:14
  • It's just to elaborate more to show the structure of the object – SuicideSheep Dec 17 '13 at 09:17
  • Does any item in [someViewModel].ColorList has ColorID value equal to [someViewModel].person.Color ? – yo chauhan Dec 17 '13 at 09:18
  • Is the problem just that when the page loads no value is selected in the ComboBox? Everything works correctly afterwards? If so, have you tried, setting the initial value for the ComboBox programatically? – jnovo Dec 17 '13 at 09:26
  • Remove the IsSynchronizedWithCurrentItem="True" attribute. While there's not enough information to diagnose the issue fully, you're probably binding multiple combo boxes with the same collection, which by setting this property will lead to the issue you're experiencing. You shouldn't need this feature given the example you've provided. – Chris Anderson Dec 18 '13 at 06:52

1 Answers1

0

Has your Color class ColorID property, or just Id?

If so: SelectedValuePath="Id"