简体   繁体   中英

ListView WPF, different background color for different items in GridViewColumn

I'm having trouble displaying different elements that should have different background color inside a ListView that implement a GridViewColumn.

I've seen this post which seems really useful : Listbox item WPF, different background color for different items

But i really don't know how to implement that into my code since the binding is made inside a GridView:

<ListView x:Name="ListBoxSelectedPlaylist" ItemsSource="{Binding Path=SelectedPlaylist.PlayableElements}" Grid.Row="1">
    <ListView.Resources>
        <ContextMenu x:Key="ContextMenu">
            <MenuItem Header="Delete item(s)" Click="ListBoxSelectedPlaylist_MenuItemDeleteItems_Click" />
        </ContextMenu>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="State" DisplayMemberBinding="{Binding ElementPlayingState}" />
            <GridViewColumn Header="Extension" DisplayMemberBinding="{Binding Extension}" />
            <GridViewColumn Header="Filename" DisplayMemberBinding="{Binding Filename}" />
            <GridViewColumn Header="Src" DisplayMemberBinding="{Binding Src}" />
        </GridView>
    </ListView.View>
</ListView>

Do you have a solution ?

If you wan to change the background of row then you can implement a style which defines the DataTrigger. For ex :

<Style TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
            <DataTrigger
                  Binding="{Binding YourProperty}"
                  Value="SomeValue">
                  <Setter Property="BorderBrush" Value="Red"/>
                  <Setter Property="Background" Value="AliceBlue"/>
             </DataTrigger>
      </Style.Triggers>
</Style>

If the logic to change the background depends on various bind properties then would recommend you to use IMultiValueConverter .

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