문제

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?

도움이 되었습니까?

해결책

Is JobPositionID a string? If not, that could explain why it doesn't work. At the databinding level, WPF won't automatically convert string to int. I'm guessing it changes the selection on the box to none when you attempt it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top