简体   繁体   中英

WP7 : Can't get Canvas.RenderTransform value

I have about 3-4 canvas controls and each contains about 750-1200 paths. Users needs to make some transform to them and I use for that a global

    Canvas SelectedCanvas;

Initially (in the constructor) SelectedCanvas takes the value of one my canvas controls.

    SelectedCanvas = canvas1;

For the button wich rotates the canvas I use the next function:

    private void RotateRightLayerButton_Click(object sender, RoutedEventArgs e)
    {
        if (SelectedCanvas.RenderTransform != null)
        {
            //method 1
            CompositeTransform ct = canvas1.RenderTransform as CompositeTransform;
            if (ct.Rotation == 360)//ct will return NullException
                ct.Rotation = 0;
            ct.Rotation += 30;

            // method 2
            TransformGroup tg = canvas1.RenderTransform as TransformGroup;                
            (tg.Children[0] as RotateTransform).Angle += 30;
            //tg will return NullException                               
        }
    }

I also tried this link and this link but I need also to get the value of RenderTransform . Am I doing something wrong? Thanks in advance!

The default value of the RenderTransform property is Transform.Identity . You have to apply a Transform, eg a RotateTransform, to your Canvas before you can manipulate it.

If you use a RotateTransform your code would have to look like this:

RotateTransform t = bd1.RenderTransform as RotateTransform; 
if (t.Angle >= 360) 
    t.Angle = 0; 
t.Angle += 30; 

or:

RotateTransform t = bd1.RenderTransform as RotateTransform; 
t.Angle = (t.Angle + 30) % 360; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM