如何将情节提要用作样式

问题描述 投票:3回答:2

我想将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> 

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

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

但是它不起作用,并引发异常。我该如何更改自己的风格?

c# wpf styles storyboard shapes
2个回答
4
投票
Storyboard.TargetProperty="Fill.Color"

并删除

© www.soinside.com 2019 - 2024. All rights reserved.