Do WPF ComboBox SelectedIndex and SelectedValue have different behavior on SelectionChanged event?

Posted by Junior Mayhé on Stack Overflow See other posts from Stack Overflow or by Junior Mayhé
Published on 2010-04-22T16:18:29Z Indexed on 2010/04/22 16:53 UTC
Read the original article Hit count: 719

Hello guys

I got this cbxJobPosition_SelectionChanged firing as expected. The problem is when a external method tries to set cbxJobPosition.

cbxJobPosition is databinded with a list of objects of type JobPosition:

  • JobPositionID: 1, JobPositionName: Manager

  • JobPositionID: 2, JobPositionName: Employee

  • JobPositionID: 3, JobPositionName: Third party

Here's the XAML:

<ComboBox Cursor="Hand" DataContext="{Binding}" ItemsSource="{Binding}"
FontSize="13" Name="cbxJobPosition"     
SelectedValuePath="JobPositionID" DisplayMemberPath="JobPositionName" 
SelectedIndex="0" Width="233" Height="23" 
SelectionChanged="cbxJobPosition_SelectionChanged" />

On UserControl_Loaded method, it reads from database the list of jobs and try to set the specific job position "Third party":

//calls cbxJobPosition_SelectionChanged and passes the correct SelectedValue within
cbxJobPosition.SelectedIndex = 3;

//calls cbxJobPosition_SelectionChanged and but won't pass the correct SelectedValue within
cbxJobPosition.SelectedValue = "3";

As you can notice, when the processing is automatically redirected to cbxJobPosition_SelectionChanged, the SelectedValue attribute will have different values for each statement above when you're debugging within cbxJobPosition_SelectionChanged event.

Does anyone know if this difference is expected, am I missing something or it could be a bug?

© Stack Overflow or respective owner

Related posts about selectedvalue

Related posts about selectedindex