WPF ComboBox SelectionChanged 事件触发两次
WPF ComboBox SelectionChanged event firing twice
在我的 DataGrid
中,我使用 DataGridComboBoxColumn
如下。它的 SelectionChanged
事件(定义如下)总是触发两次 - 一次是在我单击某个项目时,然后是当我 select 从下拉列表中选择新项目时。当我单击要更改的项目时,SelectionChanged 事件会触发并显示旧值,然后当我 select 新值时,它会再次触发并正确显示新值。但是我希望仅当我 select 组合框的新值时才触发该事件。
问题:是什么导致了这种行为,如何解决这个问题?
备注: 网上很多网友好像都发过类似的问题here but none of them helped resolve my issue - maybe, the context is a bit different here. Moreover, the XAML and the code seem ok as it correctly displays the combobox values along with the correctly combox selected values for each row in the grid. Plus, the SelectionChanged event does correctly show the newly selected value but when it fires the second time. Similar code is shown here.
<DataGridComboBoxColumn Header="StartTime" SelectedItemBinding="{Binding localTime}" ItemsSource="{StaticResource localTimeList}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<EventSetter Event="SelectionChanged" Handler="MyComboBoxColumn_SelectionChanged"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
事件:
private void MyComboBoxColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
var selectedVal = comboBox.SelectedValue.ToString();
}
What is causing this behavior?
当您进入编辑模式并且 SelectedItem
属性 被绑定到您的源 属性.
时,最初引发 SelectionChanged
事件
How can the issue be fixed?
处理此问题的最简单方法是检查 ComboBox
是否已加载,如果尚未加载,则立即从事件处理程序中直接 return:
private void MyComboBoxColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (!comboBox.IsLoaded)
return;
//handle an actual selection here...
}
在我的 DataGrid
中,我使用 DataGridComboBoxColumn
如下。它的 SelectionChanged
事件(定义如下)总是触发两次 - 一次是在我单击某个项目时,然后是当我 select 从下拉列表中选择新项目时。当我单击要更改的项目时,SelectionChanged 事件会触发并显示旧值,然后当我 select 新值时,它会再次触发并正确显示新值。但是我希望仅当我 select 组合框的新值时才触发该事件。
问题:是什么导致了这种行为,如何解决这个问题?
备注: 网上很多网友好像都发过类似的问题here but none of them helped resolve my issue - maybe, the context is a bit different here. Moreover, the XAML and the code seem ok as it correctly displays the combobox values along with the correctly combox selected values for each row in the grid. Plus, the SelectionChanged event does correctly show the newly selected value but when it fires the second time. Similar code is shown here.
<DataGridComboBoxColumn Header="StartTime" SelectedItemBinding="{Binding localTime}" ItemsSource="{StaticResource localTimeList}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<EventSetter Event="SelectionChanged" Handler="MyComboBoxColumn_SelectionChanged"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
事件:
private void MyComboBoxColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
var selectedVal = comboBox.SelectedValue.ToString();
}
What is causing this behavior?
当您进入编辑模式并且 SelectedItem
属性 被绑定到您的源 属性.
SelectionChanged
事件
How can the issue be fixed?
处理此问题的最简单方法是检查 ComboBox
是否已加载,如果尚未加载,则立即从事件处理程序中直接 return:
private void MyComboBoxColumn_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (!comboBox.IsLoaded)
return;
//handle an actual selection here...
}