首页 > 解决方案 > C# WPF - 动态改变单元格颜色

问题描述

我正在从包含 Data-Table 的 c# Data-grid 创建并用数据填充它,然后我有刷新按钮,当我单击它时,我想查看它们的值已更改为另一种颜色的单元格(红色表示例子)。我在 WPF 中很新,所以我真的不明白如何从 XML 中做到这一点,我正在从代码中创建表,所以我也尝试从代码中做到这一点。尝试了一切,单元格背景没有改变。
感谢所有愿意提供帮助的人:)

创建数据表的代码示例:

  string TID =selectedTab.Header.ToString().Split('~')[1]; // (TableID, Lvl)

            List<Tuple<string,string>> FieldList = API.getFieldsByTableID(TID); // {(Field_name,size in bits),...}

            DataGrid dg = new DataGrid();
                DataTable dt = new DataTable();

            string[] TableLevel = splitTID(TID); //TableLevel[0]=Table ;TableLevel[1]=Level;  

            string TableDump = API.GetRegs(TableLevel[0], TableLevel[1]);// Getting debug dump from simics

            #endregion

            #region *Fields_row*
            foreach (var item in FieldList)  // First line ,name of fields.
            {
                dc = new DataColumn(item.Item1, typeof(string));
                dt.Columns.Add(dc);
            }
            #endregion

            TableDump = TableDump.Split(':')[1]; // split to get just the dump
            int x = 0;
            int DumpLen = TableDump.Length; // dump length
            int EntrySize = int.Parse(API.GetEntrySize(TID)); // return entry size
            int NumOfBytes = round_bits_2_chars_amount(EntrySize);
            int count = 0;
            while (x < DumpLen)
            {
                count++;
                String str_Entry = BE_to_LE(TableDump.Substring(x, NumOfBytes));

                ulong Entry = ulong.Parse(str_Entry, System.Globalization.NumberStyles.HexNumber);

                DataRow dr = dt.NewRow();
                int row = 0;
                dr[row++] = count;
                foreach (var item in FieldList)
                {
                    int FieldLen = int.Parse(item.Item2);
                    ulong Mask =(ulong) ((1 << FieldLen) - 1);
                    ulong Value = Entry & Mask;
                    Entry = Entry >> FieldLen;
                    if (Properties.Settings.Default.IsHexadecimal)
                    {

                        dr[row] = "0x" + Value.ToString("X");
                    }
                    else
                    {
                        dr[row] =Value.ToString();
                    }

                    row += 1;
                    /*   if (int.Parse(item.Item2) > DumpLen - x)
                    {
                        x = DumpLen + 1;
                        break;
                    }
                  string FieldDump =TableDump.Substring(x,int.Parse(item.Item2));
                  x +=int.Parse(item.Item2);
                    dr[row] = long.Parse(FieldDump,System.Globalization.NumberStyles.HexNumber);
                    row +=1;*/
                }

                 dt.Rows.Add(dr);
                x += EntrySize;
            }
             dg.ItemsSource = new DataView(dt);
            selectedTab.Content = dg;
       }
    }

标签: c#wpf

解决方案


所以经过大量检查后,我找到了解决方案。

使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Nagasaki
{
    public static class Datagrid
    {
        public static DataGridRow GetSelectedRow(this DataGrid grid)
        {
            return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
        }
        public static DataGridRow GetRow(this 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;
        }
        public static DataGridCell GetCell(this 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;
        }

        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;
        }
        public static DataGridCell GetCell(this DataGrid grid, int row, int column)
        {
            DataGridRow rowContainer = GetRow(grid, row);
            return GetCell(grid, rowContainer, column);
        }

    }
}

我能够到达特定的单元格,这样我可以更改单元格的背景颜色。


推荐阅读