C#WPF - 动态更改单元格颜色 [英] C# WPF - change cell color dynamiclly

查看:85
本文介绍了C#WPF - 动态更改单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从包含数据表的c#数据网格创建并用数据填充它,然后我有刷新按钮,当我点击它时,我想看到它们的值已经改变为另一种颜色的单元格(例如红色)。我是WPF的新手,所以我真的不明白如何从XML中创建它,我从代码中创建表,所以我也试着从代码中做到这一点。尝试了一切,细胞背景也没有改变。

感谢所有想要帮助的人。



创建代码的例子DataTable:



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

List< Tuple< string,string> ;> FieldList = API.getFieldsByTableID(TID); // {(Field_name,以位为单位的大小),...}

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 ]); // 获取调试从simics转储

#endregion

#region * Fields_row *
foreach var item in FieldList) // 第一行,名称字段。
{
dc = new DataColumn(item.Item1, typeof string ));
dt.Columns.Add(dc);
}
#endregion

TableDump = TableDump.Split(' :')[ 1 ]; // 拆分以获得转储
int x = 0 ;
int DumpLen = TableDump.Length; // 转储长度
int EntrySize = int .Parse(API.GetEntrySize(TID)); // 返回条目大小
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 条目= 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&面具;
条目=条目>> FieldLen;
if (Properties.Settings.Default.IsHexadecimal)
{

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

row + = 1 ;
}

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





XAML:

< ; Window x:Name =MainWindow1x:Class =Nagasaki.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006
xmlns:local =clr-namespace:Nagasaki
mc:Ignorable =d
Title = NagasakiHeight =464.839Width =1021.708WindowStartupLocation =CenterScreenWindowStyle =ThreeDBorderWindowResizeMode =CanResizeWithGrip>
< Window.Resources>

< /Window.Resources>
< Grid Margin =0,-3,3,5.6>
< TabControl x:Name =tabControlMargin =0,5,0,-4.2Horizo​​ntalAlignment =LeftWidth =198SelectedIndex =1>
< TabItem Header =FXPMargin =99,-2,-148.2,0FontFamily =Century GothicVerticalAlignment =TopHeight =17>
< ListBox x:Name =listBoxMargin =10,7,15.8,15.4/>
< / TabItem>
< TabItem Header =ETMMargin = - 49.8,-3,0,0FontFamily =Century GothicForeground =#FF060000BorderThickness =0VerticalAlignment =TopHeight = 19Horizo​​ntalAlignment =LeftWidth =100>
< ListBox x:Name =ETM_listBoxMargin =7,10,7.8,11.4/>
< / TabItem>
< / TabControl>
< Grid x:Name =GridButtonMargin =0,0,23,8.4RenderTransformOrigin =0.5,0.5MouseDown =GridButton_MouseDownWidth =29Horizo​​ntalAlignment =RightHeight = 25VerticalAlignment =Bottom>
< Grid.Background>
< ImageBrush ImageSource =Red_Arrow_Head_Right-5121.png/>
< /Grid.Background>
< / Grid>
< Button x:Name =button2Content =Connect IAMargin =0,0,71.6,7.8Foreground =BlackBorderBrush =#FFB33434BorderThickness =0Click = button2_ClickWidth =122Height =25Horizo​​ntalAlignment =RightVerticalAlignment =BottomBackground =#FFBDC3CA/>
< Button x:Name =buttonContent =ConfigurationMargin =0,0,512.6,7.8BorderThickness =0Click =Configuration_Button_ClickRenderTransformOrigin =1.048,0.818Height =25 VerticalAlignment =BottomHorizo​​ntalAlignment =RightWidth =122Background =#FFBDC3CA/>
< TabControl x:Name =DataTabControlMargin =218,10,21.6,39.8Background =#FFBCB3B3>

< / TabControl>
< Button x:Name =CloseTabButtonContent =Close selected TabMargin =0,0,218.6,7.8Background =#FFBDC3CABorderThickness =0Click =button1_Click_1Height =25 VerticalAlignment =BottomHorizo​​ntalAlignment =RightWidth =122Foreground =Black/>
< Button x:Name =CloseAllTabButtonContent =Close All TabsMargin =0,0,365.6,7.8Background =#FFBDC3CABorderThickness =0Height =25Click =CloseAllTabButton_Click VerticalAlignment =BottomHorizo​​ntalAlignment =RightWidth =122/>
< Button x:Name =RefreshButtonContent =Refresh tableMargin =0,0,659.6,8.4BorderThickness =0Click =Refresh_Button_ClickRenderTransformOrigin =1.048,0.818Height =25 VerticalAlignment =BottomHorizo​​ntalAlignment =RightWidth =122Background =#FFBDC3CA/>
< / Grid>
< / Window>





我尝试过:



在谷歌搜索并找不到我的问题的答案。

解决方案

快速 Google搜索 [ ^ ]打开了这个可下载的例子,应该做你想要的:闪烁DataGrid单元格 - 如何正确执行 [ ^ ]


我建​​议你使用可以与at关联的CellStyle。然后,您可以在Style中使用绑定来更改DataGridColumn所需的属性。如果有必要,你可以使用Binding中的IValueConverter转换为CellStyle中背景的画笔


显然不是你,问一个问题的人......对不起混淆。

I'm creating from c# Data-grid that contain Data-Table and fill it with data,then I have refresh button that when i'm clicking it i want to see the cells that their value has been change in another color(red for example). I'm pretty new in WPF so I don't really understand how to do it from the XML and I'm creating the tables from the code so I try to do it from the code too. tried everything and the cell background is not changing.
Thank's for everyone that will try to help :)

example of the code for creating the DataTable:

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

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



XAML :

<Window x:Name="MainWindow1" x:Class="Nagasaki.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Nagasaki"
        mc:Ignorable="d"
        Title="Nagasaki" Height="464.839" Width="1021.708" WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow" ResizeMode="CanResizeWithGrip">
    <Window.Resources>

    </Window.Resources>
    <Grid Margin="0,-3,3,5.6">
        <TabControl x:Name="tabControl" Margin="0,5,0,-4.2" HorizontalAlignment="Left" Width="198" SelectedIndex="1">
            <TabItem Header="FXP" Margin="99,-2,-148.2,0" FontFamily="Century Gothic" VerticalAlignment="Top" Height="17">
                <ListBox x:Name="listBox" Margin="10,7,15.8,15.4"/>
            </TabItem>
            <TabItem Header="ETM" Margin="-49.8,-3,0,0" FontFamily="Century Gothic" Foreground="#FF060000" BorderThickness="0" VerticalAlignment="Top" Height="19" HorizontalAlignment="Left" Width="100">
                <ListBox x:Name="ETM_listBox" Margin="7,10,7.8,11.4"/>
            </TabItem>
        </TabControl>
        <Grid x:Name="GridButton" Margin="0,0,23,8.4" RenderTransformOrigin="0.5,0.5" MouseDown="GridButton_MouseDown" Width="29" HorizontalAlignment="Right" Height="25" VerticalAlignment="Bottom">
            <Grid.Background>
                <ImageBrush ImageSource="Red_Arrow_Head_Right-5121.png"/>
            </Grid.Background>
        </Grid>
        <Button x:Name="button2" Content="Connect IA" Margin="0,0,71.6,7.8" Foreground="Black" BorderBrush="#FFB33434" BorderThickness="0" Click="button2_Click" Width="122" Height="25" HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="#FFBDC3CA"/>
        <Button x:Name="button" Content="Configuration" Margin="0,0,512.6,7.8" BorderThickness="0" Click="Configuration_Button_Click" RenderTransformOrigin="1.048,0.818" Height="25" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="122" Background="#FFBDC3CA"/>
        <TabControl x:Name="DataTabControl" Margin="218,10,21.6,39.8" Background="#FFBCB3B3">

        </TabControl>
        <Button x:Name="CloseTabButton" Content="Close selected Tab" Margin="0,0,218.6,7.8" Background="#FFBDC3CA" BorderThickness="0" Click="button1_Click_1" Height="25" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="122" Foreground="Black"/>
        <Button x:Name="CloseAllTabButton" Content="Close All Tabs" Margin="0,0,365.6,7.8" Background="#FFBDC3CA" BorderThickness="0" Height="25" Click="CloseAllTabButton_Click" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="122"/>
        <Button x:Name="RefreshButton" Content="Refresh table" Margin="0,0,659.6,8.4" BorderThickness="0" Click="Refresh_Button_Click" RenderTransformOrigin="1.048,0.818" Height="25" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="122" Background="#FFBDC3CA"/>
    </Grid>
</Window>



What I have tried:

searched in google and did not find answer for my problem.

解决方案

A quick Google search[^] turned up this downloadable example that should do what you want: Flashing DataGrid Cell - How to do it properly[^]


I would recommend that you use the CellStyle which can be associated with at . You can then use binding in the Style to change what attributes you need to DataGridColumn. If necessary you can use an IValueConverter in the Binding to convert to the Brush for the Background of in the CellStyle


Obviously not you, the one asking the question...sorry for the confusion.


这篇关于C#WPF - 动态更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆