在WPF中设置listview ItemContainerStyle

时间:2017-07-03 14:24:33

标签: c# wpf listview mvvm

我有ListView行替代颜色:

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

现在我已将listview ItemContainerStyle设置如下:

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

现在的问题是listview定义ItemContainerStyle正文不止一次,所以我试图在最后定义的ItemContainerStyle="{StaticResource alternatingStyle}"部分中移动ItemContainerStyle,但我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:4)

我不确定您在问什么,但您可以使用Style属性将BasedOn基于另一个{<1}}:

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

答案 1 :(得分:-1)

最后我通过以下方式解决了这个问题:

<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解决方案也可以使用BasedOn。