12

我需要在 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;
4

2 回答 2

31

您必须将两种效果组合在一起,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;
于 2013-02-14T13:03:13.737 回答
16

如果您不使用按钮或其他机制来应用转换,则可以仅在 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>
于 2013-02-14T13:12:31.590 回答