【问题标题】:How to get the row index of a WPF Datagrid?如何获取 WPF Datagrid 的行索引?
【发布时间】:2014-02-17 09:22:51
【问题描述】:

我在 wpf 数据网格中使用图像控制列。 如果单击,该控件用于删除数据网格中的行。任何人都可以告诉我如何冒泡控件单击事件以选择网格的整行。以下是我目前的代码。

XAML 代码:

  <DataGrid x:Name="dg" >
  <DataGrid.Columns>
 <DataGridTextColumn Header="ID"  Binding="{Binding Path=ZoneId}" />
<DataGridTextColumn Header="Sector" Binding="{Binding Path=SectorIds"/>
  <DataGridTemplateColumn Width="40">
     <DataGridTemplateColumn.CellTemplate >
              <DataTemplate>                                     
       <Image x:Name="imgDel"   Source="Delete.png" Stretch="None" MouseLeftButtonDown="imgDel_MouseLeftButtonDown" />
             </DataTemplate>                                    
            </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
     </DataGrid.Columns>
    </DataGrid>

代码背后:

private void imgDel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{            
       var inx = dg.SelectedIndex;
}

我的要求是当我单击行中的图像控件时,它应该从 datacontrol 中删除整行。我的数据网格与集合绑定。

谢谢

【问题讨论】:

    标签: c# .net wpf c#-4.0


    【解决方案1】:

    你有sender,你可以用它来获取你的行索引。

    【讨论】:

    • 谢谢...但发件人将是我的图像控制。
    • 在这种情况下-您可以从dg.SelectedIndex 检索行和dg.CurrentColumn.DisplayIndex 的列-请参阅此帖子-stackoverflow.com/questions/5978053/…
    【解决方案2】:

    我有一个实用方法,用于获取 Grid 行/列。

    public static Tuple<DataGridCell, DataGridRow> GetDataGridRowAndCell(DependencyObject dep)
    {
        // iteratively traverse the visual tree
        while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
    
        if (dep == null)
            return null;
    
        if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;
    
            // navigate further up the tree
            while ((dep != null) && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
    
            DataGridRow row = dep as DataGridRow;
    
            return new Tuple<DataGridCell, DataGridRow>(cell, row);
        }
    
        return null;
    }
    

    可以这样调用:

    private void OnDoubleClick(object sender, MouseButtonEventArgs e)
            {
                DependencyObject dep = (DependencyObject)e.OriginalSource;
    
                Tuple<DataGridCell, DataGridRow> tuple = GetDataGridRowAndCell(dep);
            }
    

    【讨论】:

      【解决方案3】:

      如果您尝试获取图像所在的 DataGridRow 对象的引用,您可以使用 VisualTreeHelper 来查找它。

      private void imgDel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {            
           DependencyObject dataGridRow = sender as DependencyObject;
           while (dataGridRow != null && !(dataGridRow is DataGridRow)) dataGridRow = VisualTreeHelper.GetParent(dataGridRow);
           if (dataGridRow!= null)
           {
                 // dataGridRow now contains a reference to the row,
           }    
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-20
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 2013-12-08
        • 1970-01-01
        • 2021-12-26
        • 2011-04-28
        相关资源
        最近更新 更多