WPF轮换和选定的背景颜色不能协同工作

时间:2015-07-22 09:53:20

标签: wpf listview listviewitem selected alternation

我在ListViewItem中使用Alternation样式和选定的行背景颜色样式。我可以让它们分开工作,但它们一起工作不起作用。也许有人知道问题?

代码:

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

1 个答案:

答案 0 :(得分: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>