C# Silverlight Datagrid - 行颜色更改

Posted

技术标签:

【中文标题】C# Silverlight Datagrid - 行颜色更改【英文标题】:C# Silverlight Datagrid - Row Color Change 【发布时间】:2010-12-03 08:12:29 【问题描述】:

如何更改 silverlight 数据网格行的颜色?!

我已经尝试过了,但它似乎并没有按照我想要的方式工作...随机行的颜色不正确:

 void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        
            var c = e.Row.DataContext as Job;
            if (c != null && c.Status.Contains("complete"))
                e.Row.Background = new SolidColorBrush(Colors.Green);
            else
                e.Row.Background = new SolidColorBrush(Colors.Red);
        

【问题讨论】:

我认为使用此代码不可能获得随机颜色的行 - 但如果您不总是设置背景颜色是可能的 - 请参阅下面的答案 【参考方案1】:

微软文档:

为了提高性能,EnableRowVirtualization 属性是 默认设置为真。当 EnableRowVirtualization 属性为 设置为 true,DataGrid 不会实例化 DataGridRow 对象 绑定数据源中的每个数据项。相反,DataGrid 创建 仅在需要时使用 DataGridRow 对象,并尽可能多地重用它们 能够。例如,DataGrid 为每个数据创建一个 DataGridRow 对象 当前在视图中的项目并在其滚动时回收该行 观点。

来源:http://msdn.microsoft.com/en-gb/library/system.windows.controls.datagrid.unloadingrow.aspx

这解释了您遇到的行为

因此,正确的(虽然我承认并不容易)解决方案是使用 UnloadingRow 事件来取消设置您设置的样式。

【讨论】:

【参考方案2】:

我遇到了同样的问题,并在进行了最低限度的测试和一些演绎推理后解决了这个问题!

基本上解决方案是总是 确保设置背景颜色(或任何样式)。 不要假定行样式有任何默认值。我假设 白色的默认值 - 这是 合理的假设,但实际上并非如此。

更多细节:

看起来运行时在渲染多行时重用了 Row 类的实例。我根本没有验证这一点,但从症状来看,这似乎一定发生了。

我只有一两行应该用不同的颜色。上下滚动时,我看到随机颜色的行。

这是我做的测试课。每五行应该是红色和斜体。

您会看到几行被注释掉(设置了默认的非斜体和白色背景)。将这些注释掉 - 如果您上下滚动,您会看到很多红色!这是因为行对象正在被重用。如果你把窗口变小,然后最大化它,一些白色会回来。可能是垃圾收集器收集行,它认为您在缩小窗口后不再需要。

正如我上面所说的,解决方案是始终指定默认样式,并且不要假设任何默认值。

public partial class MainPage : UserControl

    public MainPage()
    
        InitializeComponent();

        dataGrid1.ItemsSource = Enumerable.Range(0, 50).Select(x => new Person()
        
            FirstName = "John",
            LastName = "Smith",
            ID = x,
            Delinquent = (x % 5 == 0)     // every fifth person is 'delinquent'
        );
    

    private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
    
        var person = (Person)e.Row.DataContext;

        if (person.Delinquent)
        
            e.Row.Background = new SolidColorBrush(Colors.Red);
            e.Row.Foreground = new SolidColorBrush(Colors.White);
            e.Row.FontStyle = FontStyles.Italic;
        

        else
        
           // defaults - without these you'll get randomly colored rows
           // e.Row.Background = new SolidColorBrush(Colors.Green);
           // e.Row.Foreground = new SolidColorBrush(Colors.Black);
           // e.Row.FontStyle = FontStyles.Normal;
        

    

    public class Person
    
        public string FirstName  get; set; 
        public string LastName  get; set; 
        public int ID  get; set; 
        public bool Delinquent  get; set; 
    

【讨论】:

【参考方案3】:

我在这之后:

void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        
            DataGridRow row = e.Row;
            var c = row.DataContext as Job;         
            if (c != null && c.Status.Contains("omplete"))
                e.Row.Foreground = new SolidColorBrush(Colors.Green);
            else
                e.Row.Foreground = new SolidColorBrush(Colors.Red);
        

【讨论】:

我不明白这是如何解决问题的。除非可以提出明确的问题,否则应该删除整个问题的解决方案。 @Kersny 2 天内无法接受您自己的答案。 @AnthonyWJones,像你这样惹我生气的人,长大了。 @goober - 你能解释一下这里的区别吗?正如你所描述的,我得到了随机颜色的行。它没有任何意义。我看不出它不是silverlight中的错误。如何将“完成”更改为“完成”可能解决此问题。 @goober - 好的,我知道你在这里做了什么。在我下面的发现的背景下,我仍然看不到它如何解决它。我认为也许您的原始代码是从您实际拥有的代码中“转述”的 他应该接受这个答案,因为答案对我有用,谢谢@goober【参考方案4】:

最好的方法是更改​​ DataGrid 上的 RowStyle。这需要大量的 xaml,但您可以从 here 复制并更改其中的一些样式。

另外,如果您需要根据行数据更改行颜色,您可以在样式中添加绑定到数据的 Brush 属性。

他们打开 Reflector 并从 System.Windows.Controls.Data.dll 中为 DataGrid 获取 generic.xaml,然后编写一些新的 xaml 来更改它。

【讨论】:

【参考方案5】:

它对我有用。 =)

private void MyDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    
        var row = e.Row.GetIndex();
        if (row % 2 == 0)
        
            e.Row.Background = new SolidColorBrush(Colors.Red);
            e.Row.Foreground = new SolidColorBrush(Colors.White);
            e.Row.FontStyle = FontStyles.Italic;
        

        else
        
            // defaults - without these you'll get randomly colored rows
            e.Row.Background = new SolidColorBrush(Colors.Green);
            e.Row.Foreground = new SolidColorBrush(Colors.Black);
            e.Row.FontStyle = FontStyles.Normal;
        
    

【讨论】:

以上是关于C# Silverlight Datagrid - 行颜色更改的主要内容,如果未能解决你的问题,请参考以下文章

WPF DataGrid 样式-Silverlight DataGrid?

Silverlight 2 DataGrid December 2008

在 Silverlight 中刷新 DataGrid

Silverlight- DataGrid 控件 - 选择更改事件干扰排序

在 Silverlight 中更改 Datagrid Header 的背景颜色

DataGrid和Silverlight