首页 新闻 会员 周边 捐助

WPF 数据模版中如何设定不同的背景色

0
悬赏园豆:20 [已解决问题] 解决于 2016-04-19 11:17
<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 中的颜色。

Me_Code的主页 Me_Code | 初学一级 | 园豆:52
提问于:2016-04-01 14:50
< >
分享
最佳答案
0

Background="{Binding ,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}"

写个Converter,从ColorCollection中依次取一个color,返回solidColorBrush。

收获园豆:20
德年 | 小虾三级 |园豆:1010 | 2016-04-01 15:48

Background="{Binding ??,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}"

Binding ?? 放什么?

ColorCollection 怎么依次取? ColorCollection.GetEnumerator() ?

Me_Code | 园豆:52 (初学一级) | 2016-04-01 16:04

@Me_Code: 什么都不用放。

自己写个静态的字段,标记下index.每取一次index+1。index等于ColorCollection.Count时,index重置为0

德年 | 园豆:1010 (小虾三级) | 2016-04-01 16:56

@德年: 

{Binding ,Converter=BackgroundConverter,converterParameter={staticResource ColorCollection}}

这样写报错

Me_Code | 园豆:52 (初学一级) | 2016-04-01 17:00

@德年: 自己写个静态的字段,标记下index.每取一次index+1。index等于ColorCollection.Count时,index重置为0

ColorCollection 不是仅给 StudentCollection 赋值 而已,还会同时给其它集合赋值

定义静态字段不行

Me_Code | 园豆:52 (初学一级) | 2016-04-01 17:03

@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();
        }
    }
德年 | 园豆:1010 (小虾三级) | 2016-04-01 17:08

@德年: 

你在界面上再加几个List BOX 你看看

Me_Code | 园豆:52 (初学一级) | 2016-04-01 17:09

@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>
德年 | 园豆:1010 (小虾三级) | 2016-04-01 17:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册