繁体   English   中英

如何将故事板应用为样式

[英]How to Apply Storyboard as Style

我想像这样将Storyboard应用于我的Rectangle Fill

<Rectangle Name="MyRectangle"
  Width="100"
  Height="100">
  <Rectangle.Fill>
    <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" />
  </Rectangle.Fill>
  <Rectangle.Triggers>
    <EventTrigger RoutedEvent="Rectangle.MouseEnter">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" />  
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Rectangle.Triggers>
</Rectangle> 

但我想在Style插入Storyboard ,我试过这个:

<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:s="clr-namespace:System;assembly=mscorlib" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   TargetType="{x:Type Rectangle}">

<Style.Triggers>

    <EventTrigger RoutedEvent="Shape.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>

</Style.Triggers>

<Setter Property="Shape.Fill" Value="Blue" x:Name="MySolidColorBrush"/>

</Style>

使用此代码:

var rect = new Rectangle();

using (FileStream stream = new FileStream("myStyle.xaml", FileMode.Open))
   rect.Style = XamlReader.Load(stream) as Style;

但它不起作用并引发异常。 我必须如何改变我的风格?

在您的故事板中更改此设置

Storyboard.TargetProperty="Color" 

Storyboard.TargetProperty="Fill.Color"

并删除

Storyboard.TargetName="MySolidColorBrush"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM