DataGrid通过单击UpdateSourceTrigger = SourceUpdated来捕获单元格值更改的事件 [英] DataGrid catch cell value changed event with a single click on UpdateSourceTrigger = SourceUpdated
问题描述
< 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屋!