Reputation: 139
I have an issue with my combobox. I have two windows, LPO_Process and New_LPO. LPO_Process will populate the data for New_LPO before showing it. I have two combo boxes in New_LPO, one is for the Supplier and one is for the contact person. Whenever the Supplier combobox item is changed, it will update the one for the contact person.
private void cb_Suppliers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cb_ContactPerson.DataContext = Database.LPO.Suppliers.SuppliersQuery.getContactPersonIdName(Convert.ToInt32(cb_Suppliers.SelectedValue));
cb_ContactPerson.DisplayMemberPath = "Name";
cb_ContactPerson.SelectedValuePath = "CPID";
}
Now, I'm loading data from LPO_Process using the following:
viewLPO.cb_Suppliers.SelectedValue = Convert.ToInt32((dt_LPO.Rows[0]["SID"].ToString()));
//MessageBox.Show("TEST");
viewLPO.cb_ContactPerson.Text = (dt_LPO.Rows[0]["ContactPersonID"].ToString());
viewLPO.Show();
Using debug i can see that the value passed is the required value, however when the new window opens, the contact person field is empty.
Now the weird part is, if i uncomment the message box, a message will appear before the window pops open, then after accepting, the new window will have the field populated..
It might be an issue with the UI not populating properly, however i tried several changes and nothing worked.
Any idea on how to proceed
XAML:
<ComboBox x:Name="cb_ContactPerson" HorizontalAlignment="Left" Margin="105,41,0,0" VerticalAlignment="Top" Width="279" ItemsSource="{Binding}" />
Upvotes: 0
Views: 130
Reputation: 1583
A possible cause to your issue might be your DataContext being set in the event handler, the ItemsSource
Binding reevaluation may occur slightly after the SelectionChanged event is done, causing your Text property being set before the ItemsSource is populated.
When you add a MessageBox, this will stop the execution until you dismiss the MessageBox, giving the time to the ItemsSource Binding to reevaluate.
To see this in action, check with a break point the value of ItemsSource before affecting Text property. Also, consider affecting SelectedItem instead of Text property.
EDIT: @Joey is right in his comment, edited my answer to highlight a possible cause. I was wrong when I said the event is run on a different thread.
Upvotes: 1