简体   繁体   English

Silverlight:在C#中拖动时调整控件(边框,网格等)的大小

[英]Silverlight: control (border, grid, etc) resize when drag in C#

I created (using c#) a grid with border & the parent layout is another grid. 我创建(使用C#)具有边框的网格,父布局是另一个网格。 When I try to rezise dynamically, it doesn't give the expected behaviour. 当我尝试动态调整大小时,它没有给出预期的行为。 I keep the start position (left-top) of border (with grid) fixed & only the right-bottom point is dragged to resize. 我保持边界(带有网格)的开始位置(左上角)固定,并且仅拖动右下角的点以调整大小。 In the Mouse move event, the width & height are changed depending on the current position. 在“鼠标移动”事件中,宽度和高度根据当前位置而改变。 1) But it always change the start point (left-top) when changing the width & height ? 1)但是在更改宽度和高度时,它始终会更改起点(从左上角)吗? 2) When border get resized the child (grid) doesn't change its dimensions accordingly ? 2)当边框变大时,孩子(网格)不会相应地改变其尺寸吗? I cann't find any stretching method. 我找不到任何拉伸方法。 But if border is moved, then the child grid moves with it. 但是,如果边界移动,则子网格随之移动。

    Point offsetParent;
    .....

    private void MouseMoveEvent(object sender, MouseEventArgs e)
    {
        if (bIsMouseDown)
        {
            ResizeControl(e);
            offsetParent = e.GetPosition(parentGrid); //reset offset to current                
        }
    }

    private void ResizeControl(MouseEventArgs e)
    {
        // get current point
        Point CurPosParent = e.GetPosition(parentGrid);

        // current & new position difference 
        Point diff = new Point(CurPosParent.X - offsetParent.X, CurPosParent.Y - offsetParent.Y);

        // keep start point (left-top position) of border fixed

        // adjust only width & height of border
        border1.Width += diff.X;  //changes start point (left-top position) ????
        border1.Height += diff.Y;              
    }

Found out my mistake from this link Object Positioning and Layout 从此链接中发现我的错误对象定位和布局

Now I use a Canvas as the parent. 现在,我以“画布”作为父对象。 Width & height of border & grid can be changed without changing the start point. 可以更改边框和网格的宽度和高度,而无需更改起点。

Point offsetParent; 
..... 

private void MouseMoveEvent(object sender, MouseEventArgs e) 
{ 
    if (bIsMouseDown) 
    { 
        ResizeControl(e); 
        offsetParent = e.GetPosition(parentCanvas); //reset offset to current                 
    } 
} 

private void ResizeControl(MouseEventArgs e) 
{ 
    // get current point 
    Point CurPosParent = e.GetPosition(parentCanvas); 

    // current & new position difference  
    Point diff = new Point(CurPosParent.X - offsetParent.X, CurPosParent.Y - offsetParent.Y); 

    // keep start point (left-top position) of border fixed 

    // adjust only width & height of border 
    border1.Width += diff.X;  
    border1.Height += diff.Y;   
    grid1.Width += diff.X;  
    grid1.Height += diff.Y;               
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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