locked
ListView Selected color background, and other background colors RRS feed

  • Question

  • What I am trying to do is have the background of listview rows be color coded based on the status (a property of the object it is bound to)  I am able to get this to work, but for the selected color of a row, it seems I would have to code that into each code template.  Is there a way to have the selected row background color in a different area so that I don't have to code it in each of the control templates for the different statuses?Some of my code below for when a user selects a row with the "New" status.

     

    <ControlTemplate x:Key="New"

    TargetType='{x:Type ListViewItem}'>

    <StackPanel x:Name="Bd" Background="Beige">

    <GridViewRowPresenter Content="{TemplateBinding Content}"

    Columns="{TemplateBinding GridView.ColumnCollection}"/>

    <TextBlock Background="LightBlue" Text="New" />

    </StackPanel>

    <ControlTemplate.Triggers>

    <Trigger Property="IsSelected" Value="true">

    <Setter Property="Background" TargetName="Bd">

    <Setter.Value>

    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">

    <GradientStop Offset="0" Color="Navy"

    />

    <GradientStop Offset="1" Color="#000000" />

    </LinearGradientBrush>

    </Setter.Value>

    </Setter>

    <Setter Property="Foreground" Value="{DynamicResource {xTongue Tiedtatic SystemColors.HighlightTextBrushKey}}"/>

    <Setter Property="FontWeight" Value="Bold"/>

    </Trigger>

    </ControlTemplate.Triggers>

    </ControlTemplate>

     

    <ListView Name="Tickets"

    ItemsSource="{Binding}">

    <ListView.ItemContainerStyle>

    <Style TargetType="{x:Type ListViewItem}" >

    <Setter Property="Template"

    Value="{StaticResource Default}"/>

    <Style.Triggers>

    <DataTrigger Binding="{Binding Status}" Value="In Process">

    <Setter Property="Template"

    Value="{StaticResource InProcess}"/>

    </DataTrigger>

    <DataTrigger Binding="{Binding Status}" Value="New">

    <Setter Property="Template"

    Value="{StaticResource New}"/>

    </DataTrigger>

    </Style.Triggers>

    </Style>

    </ListView.ItemContainerStyle>

    <ListView.View>

    Wednesday, July 25, 2007 4:28 PM

Answers

  • in the setter did you set TargetName="BD" so that you make sure that the setter applies to the StackPanel? if not try it out
    Friday, July 27, 2007 9:13 AM

All replies

  • try this in the Triggers for the style

    <MultiTrigger>

                                                                                                    <MultiTrigger.Conditions>

                                                                                                                        <Condition Property="IsSelected" Value="true" />

                                                                                                                        <Condition Property="Selector.IsSelectionActive" Value="true" />

                                                                                                    </MultiTrigger.Conditions>

                                                                                                    <Setter Property="Background">

                                        <Setter.Value>

                                           <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

                                             <LinearGradientBrush.GradientStops>

                                              <GradientStop Color="#0E4791" Offset="0"/>

                                              <GradientStop Color="#468DE2" Offset="1"/>

                                             </LinearGradientBrush.GradientStops>

                                           </LinearGradientBrush>

                                        </Setter.Value>

                                      </Setter>

                                                                                                    <Setter Property="Foreground" Value="White" />

                                                                                </MultiTrigger>


    Wednesday, July 25, 2007 6:17 PM
  • Thank you, but this does not allow me to change the background color.  The foreground color changes correctly.
    Wednesday, July 25, 2007 7:14 PM
  • Are you refering to the back color of the row or to the backcolor of all the listview.... sorry for not understanding 100%
    Thursday, July 26, 2007 5:45 AM
  •  

    Sorry, I meant the background color of the row.  I try to set it the above way and nothing happens, through the foreground color changes.  I think it has something to do with using a controltemplate.
    Friday, July 27, 2007 2:22 AM
  • in the setter did you set TargetName="BD" so that you make sure that the setter applies to the StackPanel? if not try it out
    Friday, July 27, 2007 9:13 AM
  • You may be able to do a binding based on the status (if it's an enum or something else that you can bind to). Bind that to the Background and use a converter to return the appropriate color. You can also use a datatrigger on the template to react to that status.. something like:

    <DataTrigger Binding="{Binding Status}" Value="New">
    <Setter Property="Background" Value="Red"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Status}" Value="In Progress">
    <Setter Property="Background" Value="Pink"/>
    </DataTrigger>

    and so on.

    Only works if you're saving your state in some compatable format, though.

    edit: I read the bottom of your code. Putting something like this in the item template should work.
    Friday, July 27, 2007 1:20 PM
  • WPF uses SystemColors.HighlightBrush to determine the selected item's background color, so you need to define your own brush and give it the key SystemColors.HighlightBrushKey.

     

    Code Snippet

    <Style TargetType='ListViewItem'>

          <Style.Resources>

            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />

          </Style.Resources>

        </Style>

     

     

    The thing I wasn't sure of (and why I didn't jump in earlier) is whether you wanted a different selected color for each type of item.  If you do, you'll need to define the brush for each type.  I'm not sure off the top of my head where to do that, but if that's your goal and you can't figure it out, let me know and I'll experiment with it.

    Friday, July 27, 2007 2:32 PM
  • carole is completly right... have a look... maybe this link can help
    http://www.beacosta.com/2007/04/power-of-styles-and-templates-in-wpf.html
    Friday, July 27, 2007 6:16 PM
  •  Marlon Grech wrote:
    in the setter did you set TargetName="BD" so that you make sure that the setter applies to the StackPanel? if not try it out

     

    I believe this was the issue.  I wasn't setting the TargetName.  Thank you.  Also thank you for the link to the solar system example.  Nice stuff.

    Friday, July 27, 2007 7:15 PM