C#WPF-动态更改单元格颜色

时间:2018-09-25 11:14:55

标签: c# wpf

我正在从包含数据表的c#数据网格中创建数据并填充数据,然后有刷新按钮,当我单击它时,我想查看其值已更改为另一种颜色的单元格(例如红色)。 我在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;
       }
    }

1 个答案:

答案 0 :(得分:0)

因此,经过大量检查,我找到了解决方案。

使用:

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

    }
}

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