0

我有一个带有一些行的 DataGrid。现在我想更改一些单元格样式,例如:|red| 赫尔|/红色| |蓝色| 罗|/蓝色| 大家。

这是我的 GetCell 函数。

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++)
            {
                var v = (Visual) VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

        public static DataGridRow GetSelectedRow(this DataGrid grid)
        {
            return (DataGridRow) grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
        }

        public static DataGridRow GetRow(this DataGrid grid, int index)
        {
            var row = (DataGridRow) grid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row != null) return row;
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow) grid.ItemContainerGenerator.ContainerFromIndex(index);
            return row;
        }

        public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
        {
            if (row == null) return null;
            var presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            var cell = (DataGridCell) presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }  
            return cell;
        }

        public static DataGridCell GetCell(this DataGrid grid, int row, int column)
        {
            DataGridRow rowContainer = grid.GetRow(row);
            return grid.GetCell(rowContainer, column);
        }

当我通过 grid.GetCell(1, 0).Content = "PpppPppp" 将 DataGridCell 内容设置为某个值,然后滚动 DataGrid 时,该值消失了!为什么?连值都跳到另一个单元格。好奇怪。

我想用样式设置单元格,所以不要使用以下功能:

(this.dataGrid1.Items[0] as DataRowView)[0] = "new value";

但:

private void SetBlockStyle(TextBlock block, Dictionary<int, int> section)
        {
            Brush foreground = Brushes.Red;
            int start = 0;
            string text = block.Text;
            block.Text = "";
            foreach (int i in section.Keys)
            {
                if (text.Length <= i)
                {
                    break;
                }
                block.Inlines.Add(text.Substring(start, i - start));
                start = i;
                block.Inlines.Add(new Run(text.Substring(i, section[i]))
                {
                    Foreground = foreground,
                    FontWeight = FontWeights.Bold
                });
                start += section[i];
            }
            if (start < text.Length)
            {
                block.Inlines.Add(text.Substring(start, text.Length - start));
            }
        }

你能给我一些建议吗?

4

0 回答 0