2

我有一个 ListView 样式,使用 WhistlerBlue.xaml 资源字典,从Codeplex 下载: WPF DataGrid Themes from Silverlight

我希望交替(奇数和偶数)行具有不同的背景颜色。

我已将以下代码添加到默认 WhistlerBlueListViewItem样式(在该 ControlTemplate.Triggers部分中):

     <Trigger Property="ItemsControl.AlternationIndex" Value="0">
          <Setter Property="Background" Value="White"></Setter>
     </Trigger>
     <Trigger Property="ItemsControl.AlternationIndex" Value="1">
          <Setter Property="Background" Value="AliceBlue"></Setter>
     </Trigger>

我还将 ListView 样式的AlternatingCount属性设置为2,但交替样式触发器仍然不起作用。

任何指针将不胜感激。

我在下面粘贴了完整的 ListViewItemStyle。

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ListViewItemFocusVisual}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Margin" Value="0,0,0,1" />
    <Setter Property="Padding" Value="5,2,5,2" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Foreground" Value="{StaticResource OutsideFontColor}"/>
    <Setter Property="Padding" Value="3" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="HoverOn">
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientOver" Storyboard.TargetProperty="Opacity" To="0.73"/>
                    </Storyboard>
                    <Storyboard x:Key="HoverOff">
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientOver" Storyboard.TargetProperty="Opacity" To="0"/>
                    </Storyboard>
                    <Storyboard x:Key="SelectedOn">
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientSelected" Storyboard.TargetProperty="Opacity" To="0.84"/>
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientSelectedDisabled" Storyboard.TargetProperty="Opacity" To="1"/>
                    </Storyboard>
                    <Storyboard x:Key="SelectedOff">
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientSelected" Storyboard.TargetProperty="Opacity" To="0"/>
                        <DoubleAnimation Duration="00:00:00.00" Storyboard.TargetName="BackgroundGradientSelectedDisabled" Storyboard.TargetProperty="Opacity" To="0"/>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Border SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" x:Name="border">
                    <Grid Margin="0">
                        <Rectangle x:Name="BackgroundGradientOver" Fill="{StaticResource hoverGradient}" Stroke="{StaticResource hoverStroke}" RadiusX="2" RadiusY="2" Opacity="0"/>
                        <Rectangle x:Name="BackgroundGradientSelectedDisabled" Fill="{StaticResource grayGradient}" Stroke="#7F8E8F8F" RadiusX="2" RadiusY="2" Opacity="0"/>
                        <Rectangle x:Name="BackgroundGradientSelected" Fill="{StaticResource BtnOverFill}" Stroke="{StaticResource selectedStroke}" RadiusX="2" RadiusY="2" Opacity="0"/>
                        <Rectangle x:Name="BackgroundHighlight" Margin="1" Stroke="#A0FFFFFF" RadiusX="1" RadiusY="1"/>
                        <GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="White"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="AliceBlue"></Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource HoverOn}"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Trigger.ExitActions>
                            <BeginStoryboard x:Name="SelectedOff_BeginStoryboard" Storyboard="{StaticResource SelectedOff}"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="SelectedOn_BeginStoryboard" Storyboard="{StaticResource SelectedOn}"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
                        <Setter Property="Visibility" TargetName="BackgroundGradientSelected" Value="Hidden"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true" />
                            <Condition Property="Selector.IsSelectionActive" Value="false" />
                        </MultiTrigger.Conditions>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

2 回答 2

3

显然,ListViewItem ControlTemplate它覆盖了交替行颜色的样式触发器。

由于我没有使用ControlTemplate(我已将它们设置Duration为 0)中的情节提要,因此我只使用样式触发器解决了这个问题,而没有设置模板的样式,如下所示。

<Style TargetType="{x:Type ListViewItem}" x:Key="ListViewItemStyle">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ListViewItemFocusVisual}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Padding" Value="5,2,5,2" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Height" Value="23px" />
        <Setter Property="Foreground" Value="{StaticResource OutsideFontColor}"/>
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="White" />
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="AliceBlue" />
                <Setter Property="BorderBrush" Value="AliceBlue" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{StaticResource BtnOverFill}" />
                <Setter Property="BorderBrush" Value="{StaticResource selectedStroke}" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="#FFbbe9fd" />
            </Trigger>
        </Style.Triggers>
</Style>

我知道如果需要设置 ContolTemplate 样式,这并不能解决问题,但至少这足以满足我的要求。

希望它对将来的人有所帮助。

于 2012-10-29T07:12:26.847 回答
0

在 ExpressionDark.xaml 例如:

找到这个:<Style TargetType="{x:Type ListViewItem}">

并找到<Rectangle x:Name="Background"..

将属性值更改FillFill="{TemplateBinding Background}"

并且,在里面<ControlTemplate.Triggers>添加这个:

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

希望这对任何人都有帮助,否则作为 wpf 的初学者,我很难找到特别是我在 vb.net 上的东西,很难使用 mvvm 获得直接示例代码等:(残酷的世界。

于 2013-02-03T18:47:37.080 回答