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

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

问题描述

我正在努力用DataGrid捕获事件。我想要实现的是,当用户在数据网格单元格的复选框上单击ONCE时,事件触发,我可以获取当前单元格值。但是,仅当选择更改时,CellChangedEvent才会触发,CellEditingEvent在单元格失去焦点时触发,或者永远不会触发。如果我尝试通过执行以下操作,使复选框变得更加容易,那么它永远不会触发:

 < DataGrid Grid。 ColumnSpan =2Grid.Row =1Grid.Column =0AutoGenerateColumns =TrueItemsSource ={Binding MasterDataTable,Mode = TwoWay}CanUserAddRows =FalseMargin =10 5CurrentCellChanged = DataGrid_CurrentCellChanged> 
< DataGrid.Resources>
< Style TargetType =DataGridCell>
< Style.Triggers>
< Trigger Property =IsMouseOverValue =True>
< Setter Property =IsEditingValue =True/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /DataGrid.Resources>
< / DataGrid>

只要用户点击单元格中的复选框,我该如何调用方法?
提前感谢

解决方案

1)在DataGrid注册表中为 TargetUpdated 事件。



2)指定一个列,理想地设置为 AutoGenerateColumns = False



3)在您的Binding标志中, NotifyOnTargetUpdated 属性(您的目标是您的复选框)。



4)在您的Binding UpdateSourceTrigger = PropertyChanged Mode = TwoWay (不是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中的

(您可能不会处理该事件)。

  private void DataGrid_TargetUpdated(object sender,DataTransferEventArgs e)
{
//做什么...
}


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) In your DataGrid register for TargetUpdated event .

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

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

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>

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天全站免登陆