DataGrid 通过单击 UpdateSourceTrigger = SourceUpdated 来捕获单元格值更改事件 [英] DataGrid catch cell value changed event with a single click on UpdateSourceTrigger = SourceUpdated

查看:20
本文介绍了DataGrid 通过单击 UpdateSourceTrigger = SourceUpdated 来捕获单元格值更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用 DataGrid 捕捉事件.我想要实现的是,当用户在数据网格单元格的复选框上单击一次时,会触发一个事件,我可以获得当前单元格值.但是 CellChangedEvent 仅在选择更改时触发,而 CellEditingEvent 要么在单元格失去焦点时触发,要么从不触发.如果我尝试通过执行以下操作使复选框可修改,它永远不会触发:

I'm struggling to catch an event with a DataGrid. What I want to achieve is that when the user clicks ONCE on a checkbox of a datagrid cell, an event fires and I can get the current cell value. However the CellChangedEvent fires only when the selection changes, and the CellEditingEvent either fires when the cell loses focus, OR never fires. It never fires if I try to make a checkbox modificable with a single click, by doing the following:

<DataGrid Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="True" ItemsSource="{Binding MasterDataTable, Mode=TwoWay}" CanUserAddRows="False" Margin="10 5" CurrentCellChanged="DataGrid_CurrentCellChanged">
            <DataGrid.Resources>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="IsEditing" Value="True" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
        </DataGrid>

如何在用户单击单元格内的复选框后立即调用方法?提前致谢.

How can I call a method as soon as the user clicks on a checkbox inside a cell? Thanks in advance.

推荐答案

1) 在您的 DataGrid 中注册 TargetUpdated 事件.

1) In your DataGrid register for TargetUpdated event .

2) 指定一个 Column ,理想情况下设置 AutoGenerateColumns=False .

2) Specify a Column , and ideally set AutoGenerateColumns=False .

3) 在您的绑定标志中,NotifyOnTargetUpdated 属性(您的目标是您的复选框).

3) In your Binding flag the NotifyOnTargetUpdated property (your target is your checkbox).

4) 在您的绑定中 UpdateSourceTrigger=PropertyChangedMode=TwoWay(不是 DataGrid 的默认行为).

4) In your Binding UpdateSourceTrigger=PropertyChanged and Mode=TwoWay (not the default behavior of the DataGrid).

XAML :

 <DataGrid TargetUpdated="DataGrid_TargetUpdated" 
           AutoGenerateColumns="False" 
           ItemsSource="{Binding SomeValues, Mode=OneWay}"  CanUserAddRows="False"  >
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding Path=.,  NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>                        
    </DataGrid.Columns>
</DataGrid>

在 CS 中:(您可能不想处理该事件.)

in CS: (where you might wan't to handle that event.)

   private void DataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
   {
         // Do what ever...
   }  

这篇关于DataGrid 通过单击 UpdateSourceTrigger = SourceUpdated 来捕获单元格值更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆