7

如何更改 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);
        }
4

5 回答 5

6

微软文档:

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

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

这解释了您所经历的行为

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

于 2010-10-18T14:25:29.077 回答
5

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

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

更多细节:

看起来运行时在呈现多行时重用了 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; }
    }
}
于 2010-01-17T02:48:42.637 回答
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);
        }
于 2009-10-02T14:27:43.440 回答
0

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

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

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

于 2011-03-23T19:36:22.383 回答
0

这个对我有用。=)

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;
        }
    }
于 2014-10-06T06:08:01.987 回答