简体   繁体   中英

How can I change the Background color of a GridViewColumn dynamically?

In the main, I've the following:

   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;

Where a Person is a class composed by a string NameP and 12 boolean, one for month.

I managed to see them in a GridView internal of a ListView. The result is the following: 在此处输入图片说明

Now, I have to change the Background in yellow of all the item which are "true", and I've no idea. This is the xaml code:

<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>

Thanks for your suggestion.

Possible duplicated of.... : No, I need to change the color dinamically, not in a static way as requested in the question that signaled me as duplicate

You can use converter to convert your Boolean value to right background

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

Converter looks like this

 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();
    }
}

Brushes in App.Resources

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

you can write style for list view

Write a Style which highlights 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM