简体   繁体   中英

DataGridCellEditEnding event is not firing when I am changing dropdown inside my datagrid in WPF

I am using the following code to use a ComboBox inside my DataGridTemplateColumn :

<DataGridTemplateColumn Header="MyHeader" Width="50">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Width="Auto" Text="{Binding Path=MyVal}" ToolTip="{Binding MyDisplayName}"></TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}" 
                      SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject}" DisplayMemberPath="MyDisplayName" 
                      FontSize="12"  >
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

CellTemplate is for showing my text (custom text for selected value) and CellEditingTemplate contains my ComboBox which has the actual list.

When I select a value from my drop-down, I have to click on another part of the data grid to get DataGridDiagnosticCellEditEnding fired.

I want to get it fired as soon as I select a value or change a value from my ComboBox .

I tried adding the following code, but it didn't work:

<ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}" 
                                 SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="MyDisplayName" FontSize="12" >

Also I tried adding IsSynchronizedWithCurrentItem="True" .

I gave a try to your code, and ve got a fix :

  1. On the ComboBox of the DataGrid Column, subscribe to the closing event

  2. Implement the event Handler and raise a routed Event.

CommitEditCommand belongs to the DataGrid class :

    private void ComboBoxDropDownClosed(object sender, EventArgs e)
    {
        DataGrid.CommitEditCommand.Execute(datagrid1,datagrid1);
    }

From there you fall in the DataGrid.CellEditting breakpoint

Here is the working solution : http://1drv.ms/1LFB5Gc

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM