我正在努力用DataGrid捕获一个事件。我想要实现的是当用户在datagrid单元格的复选框上单击ONCE时,会触发一个事件并且我可以获取当前单元格值。但是,CellChangedEvent仅在选择更改时触发,并且CellEditingEvent在单元格失去焦点时触发,或者从不触发。如果我通过执行以下操作尝试通过单击进行可修改的复选框,它永远不会触发:
<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>
如果用户点击单元格内的复选框,我该如何调用方法? 提前谢谢。
答案 0 :(得分:8)
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)
{
// Do what ever...
}
答案 1 :(得分:1)
这就是我解决它的方式。这不是最好的解决方案,但它适用于我。 如@eranotzap所述,我设置了AutoGenerateColumns = False和UpdateSourceTrigger = PropertyChanged。然后我做了以下事情:
<DataGrid Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0"
AutoGenerateColumns="False"
ItemsSource="{Binding MasterDataTable, Mode=TwoWay}"
CanUserAddRows="False"
Margin="10 5">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Fornitore}" Header="Fornitore" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=Stat}" Header="Stat" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=Intestazione}" Header="Intestazione" Width="*" IsReadOnly="True" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=PrzVend}" Header="PrzVend" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=DatEXPO}" Header="DatEXPO" Width="Auto" IsReadOnly="True" />
<DataGridCheckBoxColumn Binding="{Binding Path=Sel, NotifyOnSourceUpdated=False, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Header="Sel" Width="Auto">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="CheckBox.Checked" Handler="CellChanged"/>
<EventSetter Event="CheckBox.Unchecked" Handler="CellChanged"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsReadOnly" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="IsEditing" Value="True" />
</MultiTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Path=CodComp}" Header="CodComp" Width="Auto" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
在方法背后的代码中,每次选中或取消选中复选框时都会调用CellChanges。为了获得价值,我将执行以下操作:
void CellChanged(object sender, RoutedEventArgs e)
{
if (sender as DataGridCell != null && (sender as DataGridCell).Column != null && (sender as DataGridCell).Column.Header != null)
{
bool? isSelected = (e.OriginalSource as ToggleButton).IsChecked;
}
}
希望它对某人有帮助。
答案 2 :(得分:1)
试试这个简单的方法
private void myDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Column.SortMemberPath.Equals("EndDate"))
{
if (((MyObjectInRow)e.Row.Item).DataFine.Equals(EndDate.MinValue))
{
((MyObjectInRow)e.Row.Item).Completed = 1;
}
else
{
((MyObjectInRow)e.Row.Item).Completed = 0;
}
}
}