2009-10-02 38 views
7

如何更改silverlight datagrid行的颜色?!C#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); 
     } 
+0

我不认为它可能得到随机有色行与此代码 - 但它是可能的,如果你不总是设置背景颜色 - 见下文 – 2010-01-17 03:05:06

回答

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事件来取消你有风格组。

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

我的回答如果这对你的作品,你应该接受它作为答案。 – kersny 2009-10-02 14:41:22

+0

我不明白这是如何解决的。除非能够提出明确的问题,否则应该删除给出整个问题的解决方案。 – AnthonyWJones 2009-10-02 14:53:24

+0

@Kersny不能接受你自己的答案2天。 @AnthonyW琼斯,像你这样的人烦我,长大。 – Goober 2009-10-02 15:58:53

5

我做了一个最小的测试和一些演绎推理之后,我也遇到了同样的问题,并想出了它。

基本上解决的办法是总是 确保你设置背景颜色(或事实上任何风格)。 不要假设行造型的任何默认设置。我假设 默认为白色 - 这是合理的假设,但实际上并非如此。

更多细节:

它看起来像运行时呈现多行的时候重用行类的实例。我根本没有证实这一点,但从看起来似乎必然会发生的症状来看。

我只有一行或两行应该有不同的颜色。滚动上下滚动时,我看到随机彩色的行。

这是我做的测试班。每五行应该是红色和斜体。

您会看到一条注释掉的行(它设置了非斜体和白色背景的默认值)。有了这些评论 - 如果你上下滚动,你会看到很多红色!这是因为行对象正在被重用。如果你让窗口变小然后最大化一些白色会回来。可能垃圾收集器收集行,它不认为你需要更多的窗口后,需要更多。

正如我上面所说的,解决方案是总是指定默认样式,不要假设任何默认值。

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

这样做的最好方法是更改​​DataGrid上的RowStyle。这需要大量的xaml,但您可以从here中复制它并在其中更改一些样式。另外,如果您需要根据行数据更改行颜色,则可以将样式中的绑定添加到数据的Brush属性中。

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

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