[英]The SelectedItem of DataGrid in WPF does not fire if I switch from one ComboBox with IsEditable="True" to another
In WPF I have a DataGrid
.在 WPF 我有一个
DataGrid
。 I am using MVVM.我正在使用 MVVM。
<DataGrid x:Name="dataGrid" ItemsSource="{Binding DisplayedRows}"
SelectedItem="{Binding SelectedRow}" SelectionUnit="FullRow">
If I change row then the setter of the SelectedRow
runs as it should.如果我更改行,则
SelectedRow
的设置器将按原样运行。
There is one exception.有一个例外。 One of the columns is a
DataGridTemplateColumn
which contains ComboBox
with IsEditable="True"
其中一列是
DataGridTemplateColumn
,其中包含ComboBox
和IsEditable="True"
<DataTemplate x:Key="CategoryEditingTemplate">
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}, Path=DataContext.AppData.CategoryObjects}"
SelectedItem="{Binding Category, ValidatesOnDataErrors=True, UpdateSourceTrigger=LostFocus}"
DisplayMemberPath="FullName" IsEditable="True">
</ComboBox>
</DataTemplate>
If I start to edit the content of the ComboBox
then I click to another editable ComboBox
in another row then the setter of the SelectedRow
does not run.如果我开始编辑
ComboBox
的内容,然后单击另一行中的另一个可编辑的ComboBox
,则SelectedRow
的设置器不会运行。 I can edit the content of the second ComboBox
with keyboard.我可以用键盘编辑第二个
ComboBox
的内容。 If I click to another cell in the same row then the system realizes that the row has changed and the setter starts.如果我单击同一行中的另一个单元格,系统就会意识到该行已更改并且设置器启动。 I want the setter to run as soon as I leave a row.
我希望设置器在我离开一排后立即运行。
Update 1更新 1
Editing mode of a ComboBox
in a DataGrid
DataGrid
中ComboBox
的编辑模式
The input control ( ComboBox
) steals the focus which prevents the DataGrid
control from actually selecting the row.输入控件 (
ComboBox
) 窃取焦点,阻止DataGrid
控件实际选择行。
You could handle the PreviewMouseLeftButtonDown
event for the input control to overcome this:您可以处理输入控件的
PreviewMouseLeftButtonDown
事件以克服此问题:
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) =>
dataGrid.SelectedItem = ((ComboBox)sender).DataContext;
XAML: XAML:
<DataTemplate x:Key="CategoryEditingTemplate">
<ComboBox PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown" ItemsSource ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.