如何动态更改GridViewColumn的背景颜色?

时间:2015-09-05 22:49:45

标签: c# .net wpf visual-studio

总的来说,我有以下几点:

   List<Person> items = new List<Person>();
   Person p = new Person() { NameP = "Samantha", Gen = true, Feb = true, Mar = false, Apr = false, Mag = false, Lug = false, Ago = false, Set = false, Ott = false, Nov = false, Dic = false };
   items.Add(new Person() { NameP = "Jack", Gen = true, Feb = true, Mar = true, Apr = true, Mag = false, Lug = false, Ago = false, Set = false, Ott = false, Nov = false, Dic = false });
   listView.ItemsSource = items;

其中Person是由字符串NameP和12布尔值组成的类,一个用于月份。

我设法在ListView的GridView内部看到它们。结果如下: enter image description here

现在,我必须将所有项目的黄色背景更改为“true”,我不知道。这是xaml代码:

<ListView x:Name="listView" Height="144">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width ="150" Header="NAMEP" DisplayMemberBinding="{Binding NameP}"/>
                    <GridViewColumn Width ="50" Header="GEN" DisplayMemberBinding="{Binding Gen}"/>
                    <GridViewColumn Width ="50" Header="FEB" DisplayMemberBinding="{Binding Feb}"/>
                    <GridViewColumn Width ="50" Header="MAR" DisplayMemberBinding="{Binding Mar}"/>
                    <GridViewColumn Width ="50" Header="APR" DisplayMemberBinding="{Binding Apr}"/>
                    <GridViewColumn Width ="50" Header="MAG" DisplayMemberBinding="{Binding Mag}"/>
                    <GridViewColumn Width ="50" Header="GIU" DisplayMemberBinding="{Binding Giu}"/>
                    <GridViewColumn Width ="50" Header="LUG" DisplayMemberBinding="{Binding Lug}"/>
                    <GridViewColumn Width ="50" Header="AGO" DisplayMemberBinding="{Binding Ago}"/>
                    <GridViewColumn Width ="50" Header="SET" DisplayMemberBinding="{Binding Set}"/>
                    <GridViewColumn Width ="50" Header="OTT" DisplayMemberBinding="{Binding Ott}"/>
                    <GridViewColumn Width ="50" Header="NOV" DisplayMemberBinding="{Binding Nov}"/>
                    <GridViewColumn Width ="50" Header="DIC" DisplayMemberBinding="{Binding Dic}"/> 
                </GridView> 
            </ListView.View>
        </ListView>

感谢您的建议。

可能重复......:不,我需要改变颜色的颜色,而不是按照我所说的重复的问题中的要求以静态的方式改变颜色

2 个答案:

答案 0 :(得分:2)

您可以使用转换器将布尔值转换为右背景

<GridViewColumn Width ="50" Header="GEN" >
    <GridViewColumn.CellTemplate>
         <DataTemplate>
              <TextBlock Text="{Binding Path=Gen}" Background="{Binding Gen, Converter={StaticResource BackgroundConverter}}" />
         </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

转换器看起来像这样

 class BackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value is bool && (bool)value)
        {
            return Application.Current.FindResource("ActiveBrush");
        }

        return Application.Current.FindResource("DefaultBrush");
    }

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

App.Resources中的画笔

<SolidColorBrush x:Key="DefaultBrush" Color="Red" />
<SolidColorBrush x:Key="ActiveBrush" Color="Yellow" />

答案 1 :(得分:1)

您可以为列表视图编写样式

编写一个突出显示ListViewItems的样式                   

                <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}" Value="True">
                            <Setter Property="Background" Value="Yellow" />
                        </DataTrigger>
                    </Style.Triggers>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}"/>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="50"/>
                    <GridViewColumn Header="Date" DisplayMemberBinding="{Binding ItemID}" Width="auto" />
                    <GridViewColumn Header="ShipCity" DisplayMemberBinding="{Binding ItemSerialNumber}" Width="auto"/>
                </GridView>
            </ListView.View>
        </ListView>

Highlighting-Items-in-a-WPF-ListView