2011-02-26 79 views

回答

6

這不是建立在ItemsControl的功能。你可以做的是擴展ItemsControl的要求。

很好的例子可以發現,從here

2

我從Joe McBride了代碼,並推廣它,讓你指定什麼屬性交替(默認爲1)之前設置(默認爲Background)多少每種顏色的行,而不需要任何外部文件。

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Media; 

namespace Converters 
{ 
    public class AlternatingRowConverter : IValueConverter 
    { 
     public Brush NormalBrush { get; set; } 
     public Brush AlternateBrush { get; set; } 
     public int AlternateEvery { get; set; } 
     public string Property { get; set; } 

     public AlternatingRowConverter() 
     { 
      AlternateEvery = 1; 
      Property = "Background"; 
     } 

     public object Convert(
      object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var element = (FrameworkElement)value; 
      element.Loaded += Element_Loaded; 

      return NormalBrush; 
     } 

     public object ConvertBack(
      object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 

     private void Element_Loaded(object sender, RoutedEventArgs e) 
     { 
      var element = (FrameworkElement)sender; 

      DependencyObject obj = element; 
      do 
      { 
       obj = VisualTreeHelper.GetParent(obj); 
      } while (!(obj is ItemsControl) && obj != null); 

      var parent = (ItemsControl)obj; 
      if (parent != null) 
      { 
       DependencyObject container = 
        parent.ItemContainerGenerator.ContainerFromItem(
        element.DataContext); 

       if (container != null) 
       { 
        int index = parent.ItemContainerGenerator.IndexFromContainer(
         container); 

        if (index % (AlternateEvery * 2) >= AlternateEvery) 
         element.GetType().GetProperty(Property) 
          .SetValue(element, AlternateBrush, null); 
       } 
      } 
     } 
    } 
}