C# Silverlight Datagrid - 行颜色更改

发布于 2024-08-07 02:38:21 字数 462 浏览 2 评论 0原文

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

How do you change the color of the silverlight datagrid rows?!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

标点 2024-08-14 02:38:21

微软文档:

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

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

这解释了您所经历的行为,

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

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.

source : 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.

离笑几人歌 2024-08-14 02:38:21

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

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

更多细节:

看起来运行时在渲染多行时重用了 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; }
    }
}

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. 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; }
    }
}
打小就很酷 2024-08-14 02:38:21

我在追寻这个:

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

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);
        }
旧情别恋 2024-08-14 02:38:21

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

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

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

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 here and change a few styles in it.

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.

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.

魔法唧唧 2024-08-14 02:38:21

这对我有用。 =)

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

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文