CodingGorilla
CodingGorilla

Reputation: 19842

WPF ComboBox SelectedValue binding not updated when selection is changed

I have a Telerik RadComboBox set up like:

<t:RadComboBox Grid.Column="1" Grid.ColumnSpan="5" Grid.Row="3" x:Name="customer" IsReadOnly="True" IsEditable="True"
               ItemsSource="{Binding Path=Customers, Mode=OneWay}" DisplayMemberPath="Name" SelectedValuePath="Id"
               SelectedValue="{Binding Path=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
               SelectionChanged="customer_SelectionChanged_1"/>

The combo is properly populated, but if I click the drop down and select an item from the list, the SelectedValue property is not updated. If I begin typing in the combo and select an item that way, the SelectedValue property is not updated. BUT if I use the mouse wheel, or the arrow keys, then the SelectedValue property is properly updated.

I added the SelectionChanged event in the code behind so I could look at the properties of the control (this application is using an MVVM pattern) and it looks like that even when the SelectedItem property is set to a valid item, the SelectedValue property is null. If I use the mouse wheel to scroll down to an item, then both properties are properly set as I would expect.

Upvotes: 0

Views: 5350

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19842

It turns out that this is caused by the fact that my view model returns Guid.Empty by default when there is no selection. But since there isn't an item in the list that has an Id property of Guid.Empty the control gets confused.

Adding the attribute: SelectedIndex="0" fixes this.

Upvotes: 2

Related Questions