C# Silverlight Datagrid - 行颜色更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1509714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:17:15  来源:igfitidea点击:

C# Silverlight Datagrid - Row Color Change

c#silverlightdatagridrow

提问by Goober

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

采纳答案by David

Microsoft Documentation :

微软文档:

To improve performance, the EnableRowVirtualization property is set to true by default. 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. Instead, the DataGrid creates DataGridRow objects only when they are needed, and reuses them as much as it can. 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.

为了提高性能,EnableRowVirtualization 属性默认设置为 true。当 EnableRowVirtualization 属性设置为 true 时,DataGrid 不会为绑定数据源中的每个数据项实例化 DataGridRow 对象。相反,DataGrid 仅在需要时才创建 DataGridRow 对象,并尽可能多地重用它们。例如,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 事件来取消设置您设置的样式。

回答by Goober

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

回答by Simon_Weaver

I had this same issue and figured it out after making a minimal test and some deductive reasoning!

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

Basically the solution is to ALWAYSmake 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. I haven't verified this at all but judging from the symptoms it seems like that must be happening.

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

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

回答by Mike Blandford

The best way to do this is to change the RowStyle on your DataGrid. This requires a lot of xaml, but you can just copy that from hereand change a few styles in it.

最好的方法是更改​​ DataGrid 上的 RowStyle。这需要大量的 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.

此外,如果您需要根据行数据更改行颜色,您可以在 Style 中将绑定添加到数据的 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 来更改它。

回答by Noteah

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