<Window x:Class="WPFApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:collection="clr-namespace:System.Collections;assembly=mscorlib" xmlns:local="clr-namespace:WPFApplication" Title="MainWindow" Height="728.575" Width="894" Background="Black"> <Window.Resources> <collection:ArrayList x:Key="ColorCollection"> <SolidColorBrush Color="#FFCF1127"/> <SolidColorBrush Color="#FFFF4544"/> <SolidColorBrush Color="#FFFF7E7E"/> <SolidColorBrush Color="#FFFF99A9"/> <SolidColorBrush Color="#FFFEFB5B"/> <SolidColorBrush Color="#FFD9D757"/> <SolidColorBrush Color="#FF49D23A"/> <SolidColorBrush Color="#FF30FF66"/> <SolidColorBrush Color="#FF09FFA0"/> <SolidColorBrush Color="#FF17F1FF"/> <SolidColorBrush Color="#FF0CA1FF"/> <SolidColorBrush Color="#FF3075FF"/> <SolidColorBrush Color="#FF1111FF"/> <SolidColorBrush Color="#FF791AFF"/> <SolidColorBrush Color="#FFCB29FF"/> <SolidColorBrush Color="#FFEA1CFF"/> </collection:ArrayList> <collection:ArrayList x:Key="StudentCollection"> <local:Student Name="1"/> <local:Student Name="2"/> <local:Student Name="3"/> </collection:ArrayList> <DataTemplate x:Key="StudentDataTemplate" DataType="{x:Type local:Student}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Border Grid.Row="0" Grid.Column="0" Width="12" Height="12" BorderThickness="1" BorderBrush="White" Background="{Binding ??}"/> <Label Grid.Row="0" Grid.Column="1" FontSize="12" Foreground="White" Content="{Binding Path=Name}"/> </Grid> </DataTemplate> </Window.Resources> <Grid> <StackPanel> <ListBox Background="Black" ItemsSource="{StaticResource ResourceKey=StudentCollection}" ItemTemplate="{StaticResource ResourceKey=StudentDataTemplate}"/> </StackPanel> </Grid> </Window>
代码中:
定义了数据集合:ColorCollection
定义了实体集合:StudentCollection
定义了一个数据模版 StudentDataTemplate
问题:
如何让数据模版 StudentDataTemplate 中的 Border 背景色依次按照 ColorCollection 中的颜色。
Background="{Binding ,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}"
写个Converter,从ColorCollection中依次取一个color,返回solidColorBrush。
Background="{Binding ??,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}"
Binding ?? 放什么?
ColorCollection 怎么依次取? ColorCollection.GetEnumerator() ?
@Me_Code: 什么都不用放。
自己写个静态的字段,标记下index.每取一次index+1。index等于ColorCollection.Count时,index重置为0
@德年:
{Binding ,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}
这样写报错
@德年: 自己写个静态的字段,标记下index.每取一次index+1。index等于ColorCollection.Count时,index重置为0
ColorCollection 不是仅给 StudentCollection 赋值 而已,还会同时给其它集合赋值
定义静态字段不行
@Me_Code:
<local:BackgroundConverter x:Key="backgroundConverter"/>
<Border Grid.Row="0" Grid.Column="0" Width="12" Height="12" BorderThickness="1" BorderBrush="White" Background="{Binding Name,Converter={StaticResource backgroundConverter},ConverterParameter={StaticResource ColorCollection},Mode=OneWay}"/>
public class BackgroundConverter : IValueConverter { private static int _index = 0; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var brushes = parameter as ArrayList; var brush = (SolidColorBrush)brushes[_index]; _index++; if (_index == brushes.Count) _index = 0; return brush; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
@德年:
你在界面上再加几个List BOX 你看看
@Me_Code:
public class BackgroundConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var student = values[0]; var students = values[1] as ArrayList; var brushes = parameter as ArrayList; var index = students.IndexOf(student);
index = index % brushes.Count; var brush = brushes[index]; return brush; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
<Window.Resources> <collection:ArrayList x:Key="ColorCollection"> <SolidColorBrush Color="#FFCF1127"/> <SolidColorBrush Color="#FFFF4544"/> <SolidColorBrush Color="#FFFF7E7E"/> <SolidColorBrush Color="#FFFF99A9"/> <SolidColorBrush Color="#FFFEFB5B"/> <SolidColorBrush Color="#FFD9D757"/> <SolidColorBrush Color="#FF49D23A"/> <SolidColorBrush Color="#FF30FF66"/> <SolidColorBrush Color="#FF09FFA0"/> <SolidColorBrush Color="#FF17F1FF"/> <SolidColorBrush Color="#FF0CA1FF"/> <SolidColorBrush Color="#FF3075FF"/> <SolidColorBrush Color="#FF1111FF"/> <SolidColorBrush Color="#FF791AFF"/> <SolidColorBrush Color="#FFCB29FF"/> <SolidColorBrush Color="#FFEA1CFF"/> </collection:ArrayList> <local:BackgroundConverter x:Key="backgroundConverter" /> <collection:ArrayList x:Key="StudentCollection"> <local:Student Name="1"/> <local:Student Name="2"/> <local:Student Name="3"/> </collection:ArrayList> <DataTemplate x:Key="StudentDataTemplate" DataType="{x:Type local:Student}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Border Grid.Row="0" Grid.Column="0" Width="12" Height="12" BorderThickness="1" BorderBrush="White" > <Border.Background> <MultiBinding Converter="{StaticResource backgroundConverter}" ConverterParameter="{StaticResource ColorCollection}" > <Binding Path="{}" /> <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ListBox}" /> </MultiBinding> </Border.Background> </Border> <Label Grid.Row="0" Grid.Column="1" FontSize="12" Foreground="White" Content="{Binding Path=Name}"/> </Grid> </DataTemplate> </Window.Resources> <Grid> <StackPanel> <ListBox Background="Black" ItemsSource="{StaticResource ResourceKey=StudentCollection}" ItemTemplate="{StaticResource ResourceKey=StudentDataTemplate}"/> <ListBox Background="Black" ItemsSource="{StaticResource ResourceKey=StudentCollection}" ItemTemplate="{StaticResource ResourceKey=StudentDataTemplate}"/> <ListBox Background="Black" ItemsSource="{StaticResource ResourceKey=StudentCollection}" ItemTemplate="{StaticResource ResourceKey=StudentDataTemplate}"/> </StackPanel> </Grid>