[英]The SelectedItem of DataGrid in WPF does not fire if I switch from one ComboBox with IsEditable="True" to another
在 WPF 我有一個DataGrid
。 我正在使用 MVVM。
<DataGrid x:Name="dataGrid" ItemsSource="{Binding DisplayedRows}"
SelectedItem="{Binding SelectedRow}" SelectionUnit="FullRow">
如果我更改行,則SelectedRow
的設置器將按原樣運行。
有一個例外。 其中一列是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>
如果我開始編輯ComboBox
的內容,然后單擊另一行中的另一個可編輯的ComboBox
,則SelectedRow
的設置器不會運行。 我可以用鍵盤編輯第二個ComboBox
的內容。 如果我單擊同一行中的另一個單元格,系統就會意識到該行已更改並且設置器啟動。 我希望設置器在我離開一排后立即運行。
更新 1
輸入控件 ( ComboBox
) 竊取焦點,阻止DataGrid
控件實際選擇行。
您可以處理輸入控件的PreviewMouseLeftButtonDown
事件以克服此問題:
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) =>
dataGrid.SelectedItem = ((ComboBox)sender).DataContext;
XAML:
<DataTemplate x:Key="CategoryEditingTemplate">
<ComboBox PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown" ItemsSource ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.