【问题标题】:(WPF Datagrid) How do I determine the Column Index of an Item(WPF Datagrid)如何确定项目的列索引
【发布时间】:2011-06-18 16:48:58
【问题描述】:

当我单击一个单元格时,如何返回 WPF 数据网格中项目的列索引 我正在使用 Visual Studio 2010/VB.Net

【问题讨论】:

    标签: wpf vb.net datagrid wpf-controls


    【解决方案1】:

    DataGridCells 没有 Click 事件,它们有一个 Selected 事件,但是当您单击一个单元格时,通常会为一行中的每个单元格触发该事件。 GotFocus 可能是更好的选择。

    例如

        <DataGrid ItemsSource="{Binding Data}">
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <EventSetter Event="GotFocus" Handler="CellClick"/>
                </Style>
            </DataGrid.CellStyle>
        </DataGrid>
    

    和:

        void CellClick(object sender, RoutedEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            MessageBox.Show(cell.Column.DisplayIndex.ToString());
        }
    

    DataGridCell.Column.DisplayIndex 似乎返回了一个适当的索引,如果它还不够,你可以使用DataGrid.Columns.IndexOf(DataGridCell.Column)

    【讨论】:

    • DataGrid.Columns.IndexOf(DataGridCell.Column) 为使用演示者 (presenter.ItemContainerGenerator.ContainerFromIndex(index)) 查找单元格提供正确的索引,即使用户已移动列,列显示索引不
    【解决方案2】:

    每个人都讲述这个解决方案

    Int32 columnIndex = dataGridScannedFiles.SelectedCells[0].Column.DisplayIndex;
    

    是的,它可以工作,但没有人告诉我们必须首先为每一列设置显示索引,也许专家很容易做到这一点,但对于新手来说,这是一件陌生的事情

    有两种设置方式:-

    1. 你可以在 XAML 部分设置它。

    我不知道如何为自定义列设置它

        <DataGridTemplateColumn.CellTemplate>
                                       <DataTemplate>                                                            
        <CheckBox x:Name="ChkItem" IsChecked="{Binding Path=Sno}"/>                                
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    

    所以我更喜欢另一种方式

    1. 创建了一个函数

      私有 void SetDisplayIndexforGridViewColumns() { Int32 ColumnCount = dt.Columns.Count;

               for (int i = 0; i < ColumnCount; i++) 
               {
                   dataGridScannedFiles.Columns[i].DisplayIndex = i;
      
               }
           }
      

    dt 是我的数据表

    我正在为其分配显示索引

    现在如果你使用

    Int32 columnIndex = dataGridScannedFiles.SelectedCells[0].Column.DisplayIndex;
    

    那么你一定会得到索引

    【讨论】:

      【解决方案3】:

      您可以直接使用下面的代码来获取选定的单元格列索引。

      int index = datagrid.SelectedCells[0].Column.DisplayIndex;
      

      【讨论】:

        【解决方案4】:

        您尝试在事件点击中使用此列索引?

        int columnIndex = dataGrid.CurrentColumn.DisplayIndex;
        

        我在 MouseDoubleClick 事件或 PreviewKeyUp 中使用此代码并且运行良好。

        【讨论】:

        • 这可能适用于 Win Forms 数据网格,但不适用于 WPF 数据网格。后者没有 CurrentColumn 属性。
        • 这帮助了我,在 WPF DataGrid 上完美运行。
        【解决方案5】:

        从 DataGrid 中获取特定列名的列索引(在 wpf 中使用 linq)

          int index = DataGrid.Columns.Single(c => c.Header.ToString() == "Department").DisplayIndex;
        

        【讨论】:

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