Willy
Willy

Reputation: 10660

Setting listview ItemContainerStyle in WPF

I have a ListView which rows alternate color:

<Grid>
    <Grid.Resources>
        <Style x:Key="RowColorStyle" TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                    <Setter Property="Background" Value="LightGray" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Orange"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <ListView ItemContainerStyle="{StaticResource alternatingStyle}" AlternationCount="2">
    </ListView>

</Grid>

Now I have set listview ItemContainerStyle as below:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvi_MouseDown" />
        <EventSetter Event="PreviewMouseMove"  Handler="lvi_MouseMove" />
    </Style>
</ListView.ItemContainerStyle>

The problem now is that listview define ItemContainerStyle body more than one time so I am trying to move ItemContainerStyle="{StaticResource alternatingStyle}" within the last defined ItemContainerStyle section just implemented but I do not know how to do it.

Upvotes: 2

Views: 9203

Answers (2)

Willy
Willy

Reputation: 10660

Finally I have solved it by doing below:

<Grid>
    <Grid.Resources>
        <Style x:Key="alternatingStyle" TargetType="ListViewItem">
            <Style.Setters>
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvi_MouseDown" />
                <EventSetter Event="PreviewMouseMove"  Handler="lvi_MouseMove" />
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                    <Setter Property="Background" Value="LightGray" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Orange"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <ListView ItemContainerStyle="{StaticResource alternatingStyle}"
                      AlternationCount="2">
    </ListView>
</Grid>

@mm8 solution also works using BasedOn.

Upvotes: 0

mm8
mm8

Reputation: 169400

I am not sure what you are asking but you can base a Style on another one using the BasedOn property:

<ListView AlternationCount="2">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem" BasedOn="{StaticResource alternatingStyle}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvi_MouseDown" />
            <EventSetter Event="PreviewMouseMove"  Handler="lvi_MouseMove" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 5

Related Questions