CompositeTransform仅用于silverlight吗?

时间:2011-03-05 04:28:24

标签: wpf silverlight

CompositeTransform仅用于silverlight?无论如何我们可以在WPF或任何等效的替代品中使用它吗?

2 个答案:

答案 0 :(得分:14)

WPF中没有CompositeTransform,但有TransformGroup。因此,等效替换为TransformGroup,其中包含ScaleTransformSkewTransformRotateTransformTranslateTransform

答案 1 :(得分:0)

如果你是关于代码清洁的肛门,这是一个更好的解决方案:

http://www.singulink.com/CodeIndex/post/getting-rid-of-ugly-transformgroup-blocks-in-wpf

它很简单,因为它只返回一个TransformGroup,你仍然可以使用Blend设计器来处理变换动画!

<Rectangle Width="100" Height="100" Fill="LightGreen"
       RenderTransform="{data:CompositeTransform ScaleX=2.5, ScaleY=1, SkewX=-60, Rotation=145}"
       RenderTransformOrigin="0.5,0.5" />

实现:

public class CompositeTransformExtension : MarkupExtension
{
    public double CenterX
    {
        get { return _scale.CenterX; }
        set
        {
            _scale.CenterX = value;
            _skew.CenterX = value;
            _rotate.CenterX = value;

        }
    }

    public double CenterY
    {
        get { return _scale.CenterY; }
        set
        {
            _scale.CenterY = value;
            _skew.CenterY = value;
            _rotate.CenterY = value;
        }
    }

    public double ScaleX
    {
        get { return _scale.ScaleX; }
        set { _scale.ScaleX = value; }
    }

    public double ScaleY
    {
        get { return _scale.ScaleY; }
        set { _scale.ScaleY = value; }
    }

    public double SkewX
    {
        get { return _skew.AngleX; }
        set { _skew.AngleX = value; }
    }

    public double SkewY
    {
        get { return _skew.AngleY; }
        set { _skew.AngleY = value; }
    }

    public double Rotation
    {
        get { return _rotate.Angle; }
        set { _rotate.Angle = value; }
    }

    public double TranslateX
    {
        get { return _translate.X; }
        set { _translate.X = value; }
    }

    public double TranslateY
    {
        get { return _translate.Y; }
        set { _translate.Y = value; }
    }

    private ScaleTransform _scale = new ScaleTransform();
    private SkewTransform _skew = new SkewTransform();
    private RotateTransform _rotate = new RotateTransform();
    private TranslateTransform _translate = new TranslateTransform();

    public CompositeTransformExtension()
    {
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var group = new TransformGroup();

        group.Children.Add(_scale);
        group.Children.Add(_skew);
        group.Children.Add(_rotate);
        group.Children.Add(_translate);

        return group;
    }
}