Transforming a custom Shape in WPF

耗尽温柔 提交于 2019-12-08 04:56:56

问题


I have a custom shape in WPF that creates two rectangles in the DefiningGeometry override and returns both as a GeometryGroup as follows:

protected override System.Windows.Media.Geometry DefiningGeometry
{
    get
    {
        System.Windows.Media.GeometryGroup group = null;
        System.Windows.Media.RectangleGeometry rectangle = null;

        group = new System.Windows.Media.GeometryGroup();

        if (this.Rectangle.IsEmpty)
        {
            group.Children.Add(System.Windows.Media.Geometry.Empty);
        }
        else
        {
            rectangle = new System.Windows.Media.RectangleGeometry(new System.Windows.Rect(this.Width * 0.1, this.Height - (this.Height * 0.1), this.Width * 0.8, this.Height * 0.1), 10, this.Height * 0.1);
            group.Children.Add(rectangle);

            rectangle = new System.Windows.Media.RectangleGeometry(new System.Windows.Rect(this.Width * 0.1, this.Height - (this.Height * 0.1), this.Width * 0.8, this.Height * 0.1), 10, this.Height * 0.1);
            rectangle.Transform = new System.Windows.Media.RotateTransform(this.Tilt, this.Width / 2D, this.Height / 2D);
            group.Children.Add(rectangle);
        }

        return (group);
    }
}

The problem is I only want to apply a transform on the second rectangle but when I do that as illustrated above, it transforms other geometries in the group as well.

This is supposed to represent an animation with one static bar and one rotating (thus the transform). Any advice would be helpful.

UPDATE: I was wrong about the rotation transformation happening on both rectangles. Rotation is only happening on one. But due to that rotation, the canvas seems to translate due to which the first rectangle also moves (without rotating through). any idea what's going on here?

Here is an animated GIF. Wait for both sliders to go up and down on at a time.

The only thing changing here is the value of this.Tilt from -45 to +45 degrees.

来源:https://stackoverflow.com/questions/19890442/transforming-a-custom-shape-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!