WPF中的多个转换效果

时间:2013-02-14 13:01:41

标签: c# .net wpf transform

我需要在WPF中通过c#应用多个转换效果。

我在下面尝试了代码,但我只看到了最后一个效果,而不是两者一起。

是否可以做任何线索?

XAML

<Grid>
   <Rectangle Fill="#FFF4F4F5" Name="rect" HorizontalAlignment="Left" Height="225" Margin="84,39,0,0" Stroke="Black" VerticalAlignment="Top" Width="299"/>
</Grid>

C#

SkewTransform skewTransform1 = new SkewTransform(45, 0, -50, 50);
rect.RenderTransform = skewTransform1;

RotateTransform rotateTransform  = new RotateTransform(30);
rect.RenderTransform = rotateTransform;

2 个答案:

答案 0 :(得分:25)

您必须在TransformGroup中合并两种效果,而不是每次都覆盖它们:

var transformGroup = new TransformGroup(); 
var skewTransform = new SkewTransform(45, 0, -50, 50); 
var rotateTransform = new RotateTransform(30); 

transformGroup.Children.Add(skewTransform); 
transformGroup.Children.Add(rotateTransform); 

rect.RenderTransform = transformGroup;

答案 1 :(得分:14)

如果您没有使用按钮或其他机制来应用变换,您可以仅在XAML中定义它

    <Rectangle Fill="#FFF4F4F5"
               Name="rect"
               HorizontalAlignment="Left"
               Height="225"
               Margin="84,39,0,0"
               Stroke="Black"
               VerticalAlignment="Top"
               Width="299">
        <Rectangle.RenderTransform>
            <TransformGroup>
                <SkewTransform AngleX="45"
                               AngleY="0"
                               CenterX="-50"
                               CenterY="50" />
                <RotateTransform Angle="30" />
            </TransformGroup>
        </Rectangle.RenderTransform>
    </Rectangle>

如果添加触发器部分,则可以使其成为交互式。

    <Rectangle Fill="#FFF4F4F5"
               Name="rect"
               HorizontalAlignment="Left"
               Height="225"
               Margin="84,39,0,0"
               Stroke="Black"
               VerticalAlignment="Top"
               Width="299">
        <Rectangle.RenderTransform>
            <TransformGroup>
                <SkewTransform AngleX="45"
                               AngleY="0"
                               CenterX="-50"
                               CenterY="50" />
                <RotateTransform x:Name="RotateTransform"
                                 Angle="0" />
            </TransformGroup>
        </Rectangle.RenderTransform>
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.MouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="RotateTransform"
                                         Storyboard.TargetProperty="Angle"
                                         To="360"
                                         Duration="0:0:5"
                                         FillBehavior="Stop" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
    </Rectangle>