How to change DataGridCell value programatically

491 views Asked by At

I have a DataGrid with some rows.Now I want change some cell style, eg: |red| Hel |/red| |blue| lo |/blue| Everybody.

Here is my GetCell function.

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);
        }

When I set DataGridCell content with some value by grid.GetCell(1, 0).Content = "PpppPppp", and then scroll DataGrid, the value is gone! Why? Even the value jumps to another cell.So strange.

I want to set the cell with style, so not use the funtion about:

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

But:

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));
            }
        }

Can you give me some advice?

0

There are 0 answers