AliAzra
AliAzra

Reputation: 919

WPF ListView - Resource and ItemContainerStyle

Just a quick question. Can i use ListView.Resources and ListView.ItemContainerStyle at the same time. It seems only one of them is working if i use together..

<ListView.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding PAMStatus}" Value="ACTIVE">
                <Setter Property="Background" Value="DimGray" />
            </DataTrigger>

        </Style.Triggers>

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

Upvotes: 2

Views: 1522

Answers (1)

mm8
mm8

Reputation: 169330

It seems only one of them is working if i use together..

Yes, there can only be one style applied to the ListViewItem(s).

But you can "extend" a style by creating a new one that is based on the original one:

<ListView ...>
    <ListView.Resources>
        <Style x:Key="style" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding PAMStatus}" Value="ACTIVE">
                    <Setter Property="Background" Value="DimGray" />
                </DataTrigger>

            </Style.Triggers>
        </Style>
    </ListView.Resources>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem" BasedOn="{StaticResource style}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

How to create a style based on default style?

Upvotes: 3

Related Questions