【问题标题】:Change color to a specific cell in a row of a datagrid将颜色更改为数据网格行中的特定单元格
【发布时间】:2016-10-08 02:48:34
【问题描述】:

我在这里使用此代码部署。但是在 GetCell 中的转换,在该行中获取 null 并且无法更改单元格的颜色。我要做的是获取单元格,然后根据它们的值将单元格颜色更改为红色或绿色。

    public void ColorChange()
    {
        DataGridCell cell = GetCell(11, 1, dgLectura);
        cell.Background = new SolidColorBrush(Colors.Red);
    }

    public DataGridCell GetCell(int rowIndex, int columnIndex, DataGrid dg)
    {
        //DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;

        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
        if (row == null)
        {
            dg.UpdateLayout();
            dg.ScrollIntoView(dg.Items[rowIndex]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
        }
        DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
        DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
        return cell;
    }
    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;
    }

【问题讨论】:

    标签: c# wpf datagrid wpfdatagrid


    【解决方案1】:

    这对我有用:

    我在这里使用了 MultiConverter 来比较两个数据网格,并且我更改了每个单元格中值不同的颜色。但您可以将其用于您的场景。

     public class NameToBrushMultiValueConverter : IMultiValueConverter
     {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var dataContext = values[0];
            var dg = (DataGrid)values[1];
            var i = (DataGridCell)values[2];
            var col = i.Column.DisplayIndex;
            var row = dg.Items.IndexOf(i.DataContext);
            if (row >= 0 && col >= 0)
            {
                DataTable td1 = ((CheckXmlAppWpf.ViewModel.MainWindowViewModel)(dataContext)).GenericDataTable;
                DataTable td2 = ((CheckXmlAppWpf.ViewModel.MainWindowViewModel)(dataContext)).GenericDataTable2;
    
                if (!td1.Rows[row][col].Equals(td2.Rows[row][col]))
                {
                    GetCell(dg, row, col).Background = Brushes.Yellow;
                }
            }
    
            return SystemColors.AppWorkspaceColor;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    
    
        public DataGridCell GetCell(DataGrid dg, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dg, row);
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                if (presenter == null)
                {
                    dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                    presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                }
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                return cell;
            }
            return null;
        }
    
        public static DataGridRow GetRow(DataGrid dg, int index)
        {
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                dg.UpdateLayout();
                dg.ScrollIntoView(dg.Items[index]);
                row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }
    
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            if (parent == null) return null;
            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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多