【问题标题】:How to get the column value upon double-click in a WPF DataGrid?如何在 WPF DataGrid 中双击时获取列值?
【发布时间】:2021-03-01 12:38:49
【问题描述】:

我在 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"/>

非常欢迎任何代码/有用的文档。

我该如何做到这一点?

【问题讨论】:

  • 如果DataGridTextColumn不能很好地支持这个概念,你应该考虑实现和使用DataGridTemplateColumn

标签: c# wpf datagrid


【解决方案1】:

您可以将事件设置器添加到单元格样式中。

<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 的一种方法,这些是替代方法:

另一种方法是在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 传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多