如何在 WPF DataGrid 中双击获取列值?
How to get the column value upon double-click in a WPF DataGrid?
我在 WPF Window
中有以下 DataGrid
:
<DataGrid x:Name ="ElementDataGrid" HorizontalAlignment="Left" Height="160" Margin="10,10,0,0" VerticalAlignment="Top" Width="350" SelectionChanged="ElementDataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header ="Id" Width="*" Binding="{Binding id}">
</DataGridTextColumn>
<DataGridTextColumn Header ="Category" Width="*" Binding="{Binding categName}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我想设置它,如果我双击包含 id
的单元格,它会将我重定向到一个方法,我可以在其中描述需要发生的功能。
我尝试使用 EventSetter
,但它不适用于 DataGridTextColumn
。
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
非常欢迎任何 code/useful 文档。
我该如何完成?
您可以将事件 setter 添加到单元格样式。
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
</Style>
</DataGrid.CellStyle>
从那时起,您可以使用 sender
,即 DataGridCell
获取值。
private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var cell = (DataGridCell) sender;
var item = (YourDataItemType) cell.DataContext;
// The cell content depends on the column used.
var textBox = (TextBox) cell.Column.GetCellContent(item);
var value = textBox.Text;
// ...do something with the value
}
请注意,这只是 depends on the column used 的一种方法,这些是替代方法:
- How to read value from a Cell from a WPF DataGrid?
- WPF Datagrid Get Selected Cell Value
另一种方法是在 DataTemplateColumn
中创建您自己的数据模板并使用输入绑定。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBlock Text="{Binding id}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding id}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBox Text="{Binding id}">
<TextBox.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding categName}"/>
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
这里的一个好处是您可以直接使用命令并将值作为 CommandParameter
传递。
我在 WPF Window
中有以下 DataGrid
:
<DataGrid x:Name ="ElementDataGrid" HorizontalAlignment="Left" Height="160" Margin="10,10,0,0" VerticalAlignment="Top" Width="350" SelectionChanged="ElementDataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header ="Id" Width="*" Binding="{Binding id}">
</DataGridTextColumn>
<DataGridTextColumn Header ="Category" Width="*" Binding="{Binding categName}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我想设置它,如果我双击包含 id
的单元格,它会将我重定向到一个方法,我可以在其中描述需要发生的功能。
我尝试使用 EventSetter
,但它不适用于 DataGridTextColumn
。
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
非常欢迎任何 code/useful 文档。
我该如何完成?
您可以将事件 setter 添加到单元格样式。
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
</Style>
</DataGrid.CellStyle>
从那时起,您可以使用 sender
,即 DataGridCell
获取值。
private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var cell = (DataGridCell) sender;
var item = (YourDataItemType) cell.DataContext;
// The cell content depends on the column used.
var textBox = (TextBox) cell.Column.GetCellContent(item);
var value = textBox.Text;
// ...do something with the value
}
请注意,这只是 depends on the column used 的一种方法,这些是替代方法:
- How to read value from a Cell from a WPF DataGrid?
- WPF Datagrid Get Selected Cell Value
另一种方法是在 DataTemplateColumn
中创建您自己的数据模板并使用输入绑定。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBlock Text="{Binding id}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding id}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate DataType="{x:Type local:YourDataItemType}">
<TextBox Text="{Binding id}">
<TextBox.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding categName}"/>
</TextBox.InputBindings>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
这里的一个好处是您可以直接使用命令并将值作为 CommandParameter
传递。