4

我正在尝试为 ListView 的选定项目颜色设置动画。

我可以通过这段代码访问这个“属性”:

<Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue">
</Style.Resources>

如何为这个“属性”的颜色设置动画?

 <Storyboard x:Key="MyStoryboard">
     <ColorAnimation Storyboard.TargetName="MyList" 
                     Storyboard.TargetProperty="{x:Static SystemColors.HighlightBrushKey}"    // compilation error
                     To="Gray" Duration="0:0:1" />
 </Storyboard>

非常感谢!

4

1 回答 1

2

所以这里是 SampleStyle ;-)

它使用 ListViewItem 的模板,您可以在其中为 IsSelected-Property 的 Trigger Enter/Exit Actions 添加一个带有 ColorAnimation 的 Stoyboard!

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <Grid>
                        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        <ContentPresenter x:Name="contentPresenter" Visibility="Collapsed" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                        <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                    From="Red" To="Blue" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                    From="Blue" To="Transparent" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2013-03-27T14:37:16.193 回答