我如何编程的方法修改DataGrid行的颜色在WPF? [英] How do I programatically change datagrid row color in WPF?
问题描述
我如果可能的疑惑。我有这个数据网格包含3列和2行。 。我要的是根据我输入的数据更改URL栏的背景在我的DataGrid
I was wondering if this possible. I have this datagrid contains 3 columns and 2 rows. What I want is to change the background of the URL column on my datagrid according to the data I enter.
下面是我的XAML代码示例:
Here is sample of my xaml code:
<DataGrid
x:Name="mylistview"
BorderBrush="#FF373737"
Background="Black"
VerticalGridLinesBrush="#FF232323"
HorizontalGridLinesBrush="#FF212121">
<DataGrid.Columns>
<DataGridTextColumn Header="URL" Foreground="{Binding rowColor}" Binding="{Binding url}"/>
<DataGridTextColumn Header="Version" Foreground="White" Binding="{Binding version}"/>
<DataGridTextColumn Header="Valid" Foreground="White" Binding="{Binding valid}"/>
</DataGrid.Columns>
</DataGrid>
我的阶级用来填补与行的数据网格:
My class used to fill up the datagrid with rows:
public class myData
{
public string url {get; set;}
public string version{get; set;}
public string valid{get; set;}
//this should be the value that changes the bg color
public string rowColor{ get; set; }
}
这是我的方式行添加到控制:
And here is my way to add rows to the control:
myData data1 = new myData();
data1.url = "http://google.com";
data1.valid = "yes";
data1.version = "1";
data1.rowColor = "#FF000000";
this.mylistview.Items.Add(data1);
data1.url = "http://yahoo.com";
data1.valid = "no";
data1.version = "1";
data1.rowColor = "#000000FF";
this.mylistview.Items.Add(data1);
我有两个元素。每行应该有自己的颜色。第一个应该是红色
和第二个应该是蓝色的。我真的不知道如何控制使用rowColor值。
I have two elements. Each row should have its own color. The first one should be red and the second one should be blue. I really don't know how to make the control use the rowColor value.
什么我需要做的就是这个工作?
What do I need to do to get this working?
推荐答案
您 rowColor
不应该返回一个字符串(我将其重命名为 rowColor
,因为它是公开的),刚刚返回的实际颜色的画笔来代替。 (你的颜色是错的方式,首先是不透明的黑色,第二个是透明的蓝色)
Your rowColor
should not return a string (i would rename it to RowColor
since it is public), just return the actual colour-brushes instead. (Your colours are wrong by the way, the first is opaque black and the second is transparent blue)
例如
public Brush rowColor { get; set; }
data1.rowColor = Brushes.Red;
data2.rowColor = Brushes.Blue;
如果你真的需要从字符串进行解析时,你可以使用的值转换器或所使用的标准转换分析的XAML的时候。
If you really need to parse from string you could use a value converter or the standard conversion that is used when parsing Xaml.
除此之外我不知道如果这些特性结合前景
的列会工作。
Besides that i am not sure if binding those properties to Foreground
of the column will work.
这篇关于我如何编程的方法修改DataGrid行的颜色在WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!