簡體   English   中英

如果我從一個 IsEditable="True" 的 ComboBox 切換到另一個,則 WPF 中 DataGrid 的 SelectedItem 不會觸發

[英]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 ,其中包含ComboBoxIsEditable="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

DataGridComboBox的編輯模式在此處輸入圖像描述

輸入控件 ( 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM