0

我在 ListViewItem 中与交替样式和选定的行背景颜色样式作斗争。我可以让它们单独工作,但它们一起不起作用。也许有人知道问题?

代码:

<AlternationConverter x:Key="BackgroundConverter">
        <SolidColorBrush Color="#19f39611" />
        <SolidColorBrush Color="#19000000" />
    </AlternationConverter>

    <Style x:Key="Style2" TargetType="ListViewItem">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border 
                        Name="Border"
                        Padding="2"
                        SnapsToDevicePixels="true"
                        Background="Transparent">
                        <GridViewRowPresenter
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border"
                                    Property="Background" Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" 
                                    Value="Beige"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="Style1" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource Style2}">
        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self},
             Path=(ItemsControl.AlternationIndex),
             Converter={StaticResource BackgroundConverter}}"/>
    </Style>

......

<ListView  AlternationCount="2" ItemContainerStyle="{StaticResource Style1}" >
4

1 回答 1

1

我找到了其他方法:

<Style  TargetType="{x:Type ListViewItem}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>

        <Setter Property="FontSize" Value="21"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border Background="{TemplateBinding Background}" BorderThickness="1"  BorderBrush="#E6E6E6"
                            Name="Border" Padding="2"  SnapsToDevicePixels="true">
                        <GridViewRowPresenter Content="{TemplateBinding Content}"
                                              Columns="{TemplateBinding GridView.ColumnCollection}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        </GridViewRowPresenter>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border"
                                    Property="Background" Value="#FFD65E"/>
                            <Setter TargetName="Border" Property="BorderThickness" Value="1"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="#FFBA59"/>
                        </Trigger>


                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <!-- setting up triggers for alternate background colors -->
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="#FFFFFF"></Setter>

            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#F7F7F7"></Setter>

            </Trigger>
        </Style.Triggers>
    </Style>
于 2015-07-22T12:24:38.403 回答