C#中的Silverlight的Datagrid - 行颜色变化(C# Silverlight D

2019-07-20 14:06发布

你如何改变的Silverlight的DataGrid行的颜色?

我已经试过这一点,但它似乎没有工作,我怎么想它...随机行会涂有颜色不正确:

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

Answer 1:

微软文档:

为了提高性能,EnableRowVirtualization属性默认设置为true。 当EnableRowVirtualization属性被设置为true时,数据网格不会实例在绑定数据源中的每个数据项的DataGridRow对象。 相反,DataGrid中创建在需要的时候DataGridRow对象只和重用他们尽可能多的,因为它可以。 例如,数据网格创建用于每个数据项中当前可以视图和回收的行,当它滚出视DataGridRow对象。

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

这说明你已经遇到的行为

正确的(虽然不容易,我承认)溶液中,因此,使用UnloadingRow事件来取消您已设置的样式。



Answer 2:

我有同样的问题,使最小的测试和一些演绎推理后想通了!

基本上解决的办法是始终确保您设置背景颜色(或事实上任何风格)。 不要承担排造型任何违约 。 我是假设的白色默认 - 这是一个合理的假设,但事实并非如此。

更多细节:

它看起来像运行时呈现多行的时候重用行类的实例。 我还没有在所有的验证了这一点,但是从它似乎是必须发生的症状判断。

我只有一个或两行应该被不同颜色。 向上和向下滚动,当我看到随机有色行。

下面是我的测试类我做了。 每个第五行应该是红色和斜体。

你会看到注释掉两行(即设置的非斜体和白色背景上的缺省值)。 有了这些注释 - 如果你上下滚动,你会看到很多的红色! 这是因为该行对象是被重用。 如果使窗口小,然后最大化它的一些白色的会回来的。 大概垃圾收集器收集行不认为你需要更多的具有由窗口小了。

正如我上述解决方案表示是要始终指定默认的风格和不承担任何违约。

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


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


Answer 4:

要做到这一点,最好的办法是改变你的DataGrid中的RowStyle。 这需要大量的XAML的,但你可以复制,从这里 ,并在其中改变一些风格。

另外,如果您需要更改基于行的数据行的颜色,你可以添加在样式绑定到你的数据的画笔属性。

他们打开了反射器,把generic.xaml为DataGrid从System.Windows.Controls.Data.dll,然后写了一些新的XAML来改变它。



Answer 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 - Row Color Change