1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
namespace WpfAlternationItemsControl
{
public class GetIndexToBrushMultiConverter : DependencyObject , IMultiValueConverter
{
public GetIndexToBrushMultiConverter()
{
Borders = new List<SolidColorBrush>();
Borders.Add(Brushes.Black); //brush par defaut
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var ctx = values[1];
if (ctx == null) return 0;
var collection = (CollectionView)
CollectionViewSource.GetDefaultView(ctx);
var itemIndex = collection.IndexOf(values[0]);
if (itemIndex % Borders.Count == 0) //multiple
{
return Borders[0];
}
else // sinon l'index du brush est modulo Borders.Count
{
return Borders[itemIndex % Borders.Count];
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException("GetIndexMultiConverter_ConvertBack");
}
public List<SolidColorBrush > Borders
{
get { return (List<SolidColorBrush>)GetValue(BordersProperty); }
set { SetValue(BordersProperty, value); }
}
// Using a DependencyProperty as the backing store for Borders. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BordersProperty =
DependencyProperty.Register("Borders", typeof(List< SolidColorBrush>),
typeof(GetIndexToBrushMultiConverter),
new FrameworkPropertyMetadata (null));
}
} |
Partager