简体   繁体   English

C#Silverlight Datagrid - 行颜色更改

[英]C# Silverlight Datagrid - Row Color Change

How do you change the color of the silverlight datagrid rows?! 你如何改变silverlight数据网格行的颜色?!

I've tried this but it doesn't seem to work how I want it to...Random rows get colored incorrectly: 我试过这个,但它似乎没有用,我想要它...随机行变色不正确:

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

Microsoft Documentation : Microsoft文档:

To improve performance, the EnableRowVirtualization property is set to true by default. 为了提高性能,默认情况下EnableRowVirtualization属性设置为true。 When the EnableRowVirtualization property is set to true, the DataGrid does not instantiate a DataGridRow object for each data item in the bound data source. 当EnableRowVirtualization属性设置为true时,DataGrid不会为绑定数据源中的每个数据项实例化DataGridRow对象。 Instead, the DataGrid creates DataGridRow objects only when they are needed, and reuses them as much as it can. 相反,DataGrid仅在需要时才创建DataGridRow对象,并尽可能多地重用它们。 For example, the DataGrid creates a DataGridRow object for each data item that is currently in view and recycles the row when it scrolls out of view. 例如,DataGrid为当前处于视图中的每个数据项创建一个DataGridRow对象,并在它滚出视图时回收该行。

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

this explains the behaviour you have been experiencing 这解释了您遇到的行为

the proper (though not easier I admit) solution being, hence, to use the UnloadingRow event to unset the style you had set. 因此,正确的(虽然不容易让我承认)解决方案是使用UnloadingRow事件取消设置您设置的样式。

I had this same issue and figured it out after making a minimal test and some deductive reasoning! 我有同样的问题,并在做了一个最小的测试和一些演绎推理后想出来了!

Basically the solution is to ALWAYS make sure you set the background color (or any style in fact). 基本上解决方案是始终确保您设置背景颜色(或任何样式)。 Don't assume any defaults for row styling . 不要假设行样式的任何默认值 I was assuming a default of white - which is a reasonable assumption but was not actually the case. 我假设默认为白色 - 这是一个合理的假设,但事实并非如此。

More details: 更多细节:

It looks like the runtime reuses instances of the Row class when rendering multiple rows. 看起来运行时在渲染多行时会重用Ro​​w类的实例。 I haven't verified this at all but judging from the symptoms it seems like that must be happening. 我根本没有证实这一点,但从似乎必须发生的症状判断。

I had only one or two rows that ought to be colored differently. 我只有一两行应该有不同的颜色。 I was seeing randomly colored rows when scrolling up and down. 当上下滚动时,我看到随机颜色的行。

Here is my test class i made. 这是我制作的测试课。 Every fifth row is supposed to be red and italic. 每隔五行应该是红色和斜体。

You'll see a couple lines commented out (that set a default of non-italic and white background). 你会看到一些注释掉的行(设置默认的非斜体和白色背景)。 With these commented out - if you scroll up and down you will see a lot of red!! 随着这些注释 - 如果你上下滚动你会看到很多红色!! This is because the row objects are being reused. 这是因为正在重用行对象。 If you make the window smaller and then maximize it some of the white will come back. 如果你让窗户变小,然后最大化它,一些白色会回来。 Probably garbage collector collecting rows it doesn't think you'll need any more after having made the window smaller. 可能是垃圾收集器收集行,它认为在使窗口变小之后你将不再需要它。

As i said above the solution is to always specify styles for defaults and don't assume any defaults. 正如我上面所说,解决方案是始终为默认值指定样式,并且不假设任何默认值。

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

I was after this: 我是在这之后:

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

It works for me. 这个对我有用。 =) =)

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

The best way to do this is to change the RowStyle on your DataGrid. 执行此操作的最佳方法是更改​​DataGrid上的RowStyle。 This requires a lot of xaml, but you can just copy that from here and change a few styles in it. 这需要大量的xaml,但你可以从这里复制它并更改其中的一些样式。

Also, if you need to change the row color based on the row data, you can add a binding in the Style to a Brush property on your data. 此外,如果需要根据行数更改行颜色,可以在样式中将绑定添加到数据的Brush属性中。

They opened Reflector and took generic.xaml for the DataGrid from System.Windows.Controls.Data.dll, and then wrote some new xaml to change it. 他们打开Reflector并从System.Windows.Controls.Data.dll获取DataGrid的generic.xaml,然后编写了一些新的xaml来更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM