我有一个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>

现在的问题是,列表 View 多次定义了ItemContainerStyle主体,因此我试图在刚刚实现的最后一个定义的ItemContainerStyle="{StaticResource alternatingStyle}"部分中移动ItemContainerStyle,但我不知道该怎么做。

最佳答案

我不确定您要问的是什么,但是您可以使用Style属性将BasedOn基于另一个:

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

关于c# - 在WPF中设置listview ItemContainerStyle,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44888051/

10-10 14:52