【问题标题】:Getting datagridrow from a datarowview WPF从 datarowview WPF 获取 datagridrow
【发布时间】:2012-11-20 09:58:34
【问题描述】:

我正在尝试遍历数据网格中的每一行,提取一个列值,将该值传递给一个方法并根据该方法的结果设置该行的样式。

在发现我不能只循环遍历数据网格的行后,我发现 this 帖子详细说明了它是如何实现的。

我稍作修改,以便使用 datarowview 对象。

我现在遇到的问题是

var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;

总是返回 null。

请有人告诉我为什么会发生这种情况,以及他们是否能找到一种更简单的方法。

如果您需要更多信息,请告诉我。

这是我的代码:

private void colorArchived( DataGrid grid , GX3MaterialSelectionData data)
    {
        var row = GetDataGridRows(grid);
        foreach (DataRowView r in row)
        {
            var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;
            int val = int.Parse(r.Row[0].ToString());
            if ( data.IsArchived(val) )
            {
                // style will be defined in xaml
                dgRow.Style = mystyle;
            }


        }

    }

    public IEnumerable<DataRowView> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = item;
            if (null != row) yield return (DataRowView)row;
        }
    }

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    根据您的问题,我刚刚更新了上述 StyleSelector 类:

    public class RowStyle : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var dgRow = item as DataGridRow;
            int val = int.Parse(dgRow.Row[0].ToString());
            if ( data.IsArchived(val) )
            {
                return Mystyle;
            }
            return base.SelectStyle(item, container);
        }
    
        // style will be defined in xaml
        public Style Mystyle
        {
            get;
            set;
        }
    }
    

    注意:将“GX3MaterialSelectionData 数据”描述为类的静态,以便上述类可以直接访问它。

    【讨论】:

      【解决方案2】:

      在这种情况下,您可以使用 StyleSelector。

      public class RowStyle : StyleSelector
      {
          public override Style SelectStyle(object item, DependencyObject container)
          {
              // here the item property is the entity that the grid row is bound to.
              // check whatever values you want on it and locate a matching style with
              // find resource.
      
              // return a reference to the correct style here
      
              // or allow this to run if you want the default style.
              return base.SelectStyle(item, container);
          }
      }
      

      要在数据网格上使用它,您需要设置 RowStyleSelector 属性。

      <Window x:Class="Rich.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:local="clr-namespace:Rich"
              DataContext="{Binding RelativeSource={RelativeSource Self}}"
              Title="MainWindow" Height="350" Width="525">
          <Window.Resources>
              <local:RowStyle x:Key="styleSelector"/>
          </Window.Resources>
          <Grid>
              <DataGrid ItemsSource="{Binding Items}" RowStyleSelector="{StaticResource styleSelector}">            
                  <DataGrid.Columns>
                      <DataGridTextColumn Header="test" Binding="{Binding Test1}"/>
                      <DataGridTextColumn Header="test2" Binding="{Binding Test2}"/>
                      <DataGridTextColumn Header="test3" Binding="{Binding Test3}"/>
                  </DataGrid.Columns>
              </DataGrid>
          </Grid>
      </Window>
      

      【讨论】:

        【解决方案3】:

        方法一:

        //simple way using SelectionChanged or RowEditEnding event without DataRowView
                DataRowView row = (DataRowView)t_DataGrid.SelectedItems[0];
        
                DataGridRow row1 = e.Row;
        

        方法 2:

            //convert  datagridrow to datarowview (it works sometimes only databinding problems)
            DataGridRow dgr = FindVisualParent<DataGridRow>(txtPcode);
            DataRowView drv = dgr.DataContext as DataRowView;
        

        方法 3:

        //it needs improvements
        DataRowView dataRow = (DataRowView)t_DataGrid.SelectedItem;
        //subitem
        int i_cell = t_DataGrid.CurrentCell.Column.DisplayIndex;
        int i= int.Parse(dataRow.Row.ItemArray[id_cell].ToString());
        //get datagrid by index
        DataGridRow dgr = DG.GetRow(t_DataGrid_temp, i);
        
        //create this special Datagrid class for getting full access over datagrid
        public static class DG
        {
            /* Get the Cell using Row Index and Column Index */
            public static DataGridCell GetCell(DataGrid grid, int row, int column)
            {
                DataGridRow rowContainer = GetRow(grid, row);
                return GetCell(grid, rowContainer, column);
            }
        
            /* Get the DataGridRow using Row Index */
            public static DataGridRow GetRow(DataGrid grid, int index)
            {
                DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
                if (row == null)
                {
                    // May be virtualized, bring into view and try again.
                    grid.UpdateLayout();
                    grid.ScrollIntoView(grid.Items[index]);
                    row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
                }
                return row;
            }
        
            /* Get the Cell using DataGridRow Object and Column Index */
            public static DataGridCell GetCell(DataGrid grid, DataGridRow row, int column)
            {
                if (row != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
        
                    if (presenter == null)
                    {
                        grid.ScrollIntoView(row, grid.Columns[column]);
                        presenter = GetVisualChild<DataGridCellsPresenter>(row);
                    }
        
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    return cell;
                }
                return null;
            }
        
            /* Get the Child Element Based on Type */
            public static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
        
            /* Get the Child by name */
            public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null) return null;
        
                T foundChild = null;
        
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
                    if (childType == null)
                    {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
                return foundChild;
            }
            /*get parent*/
            public static T GetParent<T>(DependencyObject d) where T : class
            {
                while (d != null && !(d is T))
                {
                    d = VisualTreeHelper.GetParent(d);
                }
                return d as T;
            }
        
            //new
            public static Visual GetChildrenByType(Visual visualElement, Type typeElement, string nameElement)
            {
                if (visualElement == null) return null;
                if (visualElement.GetType() == typeElement)
                {
                    FrameworkElement fe = visualElement as FrameworkElement;
                    if (fe != null)
                    {
                        if (fe.Name == nameElement)
                        {
                            return fe;
                        }
                    }
                }
                Visual foundElement = null;
                if (visualElement is FrameworkElement)
                    (visualElement as FrameworkElement).ApplyTemplate();
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualElement); i++)
                {
                    Visual visual = VisualTreeHelper.GetChild(visualElement, i) as Visual;
                    foundElement = GetChildrenByType(visual, typeElement, nameElement);
                    if (foundElement != null)
                        break;
                }
                return foundElement;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-02
          • 2011-04-09
          • 1970-01-01
          • 2013-06-09
          • 2023-04-02
          • 2023-04-08
          • 2020-07-04
          • 1970-01-01
          相关资源
          最近更新 更多