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
J开发者_运维问答obPositionID: 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.
精彩评论