2013-03-27 23 views
4

我正在尝试为ListView的选定项目颜色设置动画。WPF C#如何动画HighlightBrush颜色?

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

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

非常感谢!

+1

我不认为你可以...静态资源设置,一旦应用程序加载,即使你没有改变它,我不认为绑定到它的对象会得到重新评估,因为他们可能使用一个'{StaticResource ...}'并且期望值是静态的。 – Rachel 2013-03-27 14:20:32

+0

是的rachel是对的,但是你可以为ListViewItem创建一个样式,并且在那里你可以添加一个coloranimation如果该项目被选中了......我会尽快回复你的问题并且带有一个样例代码;) – makim 2013-03-27 14:24:02

+0

@sine谢谢,我会等着 – Guilherme 2013-03-27 14:25:12

回答

2

因此,这里是SampleStyle ;-)

它使用的ListViewItem的模板中,你可以在触发加Stoyboard与ColorAnimation进入/退出操作的IsSelected,物业!

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

作品像魅力。谢谢! – Guilherme 2013-03-27 19:35:25