Changing Position of an Element Programmatically in WPF

前端 未结 6 1285
时光说笑
时光说笑 2020-12-16 11:40

I did not knew that this simple thing would be slightly complicated. I have a Canvas in which I am trying to add Ellipse dynamically. Here is the code:

<         


        
6条回答
  •  [愿得一人]
    2020-12-16 12:13

    if you want to move your canvas with matrix you should do like this :

    
       
         
       
    
    

    then you can do all works you want, on Matrix "mt". Like this :

    For Scale:

    Matrix matrix = new Matrix();
    matrix.Scale(1.5, 1.5);
    mt.Matrix = matrix;
    mcanvas.LayoutTransform = Transform.Identity;
    

    For Translate ( Changing Position ) :

    Matrix matrix = new Matrix();
    matrix.Translate(50, 0);
    mt.Matrix = matrix;
    mcanvas.LayoutTransform = Transform.Identity;
    

    And if you want to create a canvas element programmatically, you should do like this :

    Ellipse el = new Ellipse();
    Matrix matrix = new Matrix();
    matrix.Translate(50, 0);
    matrix.Scale(1.5,1.5);
    el.RenderTransform = new MatrixTransform(matrix);
    

    Hope this helps you.

提交回复
热议问题