UIElement移动过程中的奇怪行为。为什么?

时间:2013-03-08 14:17:33

标签: c# .net wpf uielement wpf-positioning

我在理解(和修复)我遇到的错误方面遇到了一些麻烦。

我有如下图所示的用户界面:

所有这些浅蓝色区域都是画布,它们是可移动的。这是我遇到问题的地方。左上角的那个可以毫无问题地移动。当我拖动它们时,另外两个消失了。我无法解释原因。

这是移动元素的代码:

// this is all inside the MouseMove event handler function

// If there is no dragged element
if (this.DraggedElement == null || !this.IsDragInProgress)
    return;

/*
 * Calculating the new position for the dragged element
 */

// Mouse current position
Point cursor = e.GetPosition(this);

double xMove = 0;
double yMove = 0;

// Movement detected
if (cursor != MouseClickLocation)
{
    // Moving on the x-axis and y-axis
    xMove = cursor.X - MouseClickLocation.X;
    yMove = cursor.Y - MouseClickLocation.Y;

    // Actually moving the element
    if (this.ConstrainToBounds(this.DraggedElement, mainWindow))
    {
        TranslateTransform translate = new TranslateTransform(xMove, yMove);

        this.DraggedElement.RenderTransform = translate;
    }
}

ConstrainToBounds()方法的代码不允许我在窗口边框外移动任何Canvas(它适用于左上方画布,但不适用于其他画布),如下所示:

private Boolean ConstrainToBounds(Canvas element, UIElement container)
{
    try
    {
        Boolean respects = true;

        // Values used to reset the element position to a proper location
        double xReset = 0;
        double yReset = 0;

        // Left x-axis constraint
        if (element.TranslatePoint(new Point(), container).X <= new Point(0, 0).X)
        {
            respects = false;

            // Get elements' current position and adjust
            xReset = element.TranslatePoint(new Point(), container).X + 1;
            yReset = element.TranslatePoint(new Point(), container).Y;

            TranslateTransform translate = new TranslateTransform(xReset, yReset);

            element.RenderTransform = translate;
        }

        // Right x-axis constraint
        if (element.TranslatePoint(new Point(), container).X + element.RenderSize.Width >= container.RenderSize.Width)
        {
            respects = false;

            // Get elements' current position and adjust
            xReset = element.TranslatePoint(new Point(), container).X - 1;
            yReset = element.TranslatePoint(new Point(), container).Y;

            TranslateTransform translate = new TranslateTransform(xReset, yReset);

            element.RenderTransform = translate;
        }

        // Top y-axis constraint
        if (element.TranslatePoint(new Point(), container).Y <= new Point(0, 0).Y)
        {
            respects = false;

            // Get elements' current position and adjust
            xReset = element.TranslatePoint(new Point(), container).X;
            yReset = element.TranslatePoint(new Point(), container).Y + 1;

            TranslateTransform translate = new TranslateTransform(xReset, yReset);

            element.RenderTransform = translate;
        }

        // Bottom y-axis constraint
        if (element.TranslatePoint(new Point(), container).Y + element.RenderSize.Height >= container.RenderSize.Height)
        {
            respects = false;

            // Get elements' current position and adjust
            xReset = element.TranslatePoint(new Point(), container).X;
            yReset = element.TranslatePoint(new Point(), container).Y - 1;

            TranslateTransform translate = new TranslateTransform(xReset, yReset);

            element.RenderTransform = translate;
        }

        return respects;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Edit_1:从MainWindow.xaml添加了代码:

<Window 
Name="mainWindow"
x:Class="WPF_TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="480" Width="555">

<Grid Name="mainGrid">
    <Canvas Grid.Row="0" 
            Grid.Column="0" 
            Background="AliceBlue" 
            Name="LeftTop"
            MouseDown="onMouseDown"
            MouseMove="onMouseMove" 
            MouseUp="onMouseUp" >

        <Ellipse Fill="Blue"
       Width="100"
       Height="100"/>
    </Canvas>

    <Canvas Grid.Row="0" 
            Grid.Column="2" 
            Background="AliceBlue" 
            Name="RightTop"
            MouseDown="onMouseDown"
            MouseMove="onMouseMove"
            MouseUp="onMouseUp">
        <Ellipse Fill="Blue"
       Width="100"
       Height="100"/>
    </Canvas>

    <Label Grid.Row="2" 
            Grid.Column="0"  
            Name="LeftBottom">
    </Label>

    <Canvas Grid.Row="2" 
            Grid.Column="3" 
            Background="AliceBlue" 
            Name="RightBottom"
            MouseDown="onMouseDown"
            MouseMove="onMouseMove"
            MouseUp="onMouseUp">
        <Ellipse Fill="Blue"
       Width="100"
       Height="100"/>
    </Canvas>

    <Grid.RowDefinitions>
        <RowDefinition Height="200" />
        <RowDefinition Height="50" />
        <RowDefinition Height="200" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="250" />
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="250" />
    </Grid.ColumnDefinitions>
</Grid>
</Window>

Edit_2:所以,我发现当我移动右上方画布时,它实际上移动到视图之外(位置600,0)。目前正试图理解为什么会这样。

1 个答案:

答案 0 :(得分:0)

可能更容易更新Canvas的Margin属性,而不是使用渲染变换:

if (cursor != MouseClickLocation)
{
    // Moving on the x-axis and y-axis
    xMove = cursor.X - MouseClickLocation.X;
    yMove = cursor.Y - MouseClickLocation.Y;

    // Actually moving the element
    this.DraggedElement.Margin = this.CalculateNewPosition(this.DraggedElement, mainWindow, xMove, yMove);
}

CalculateNewPosition可能看起来像这样(警告,未经测试):

private Thickness CalculateNewPosition(Canvas element, UIElement container, double translationX, double translationY)
{
    Thickness currentPosition = element.Margin;
    Thickness newPosition = new Thickness(currentPosition.Left + translationX, currentPosition.Top + translationY, 0, 0);

    int containerWidth = container.ActualWidth;
    int containerHeight = container.ActualHeight;
    int elementWidth = element.ActualWidth;
    int elementHeight = element.ActualHeight;

    if (newPosition.Left < 0)
        newPosition.Left = 0;
    else if (newPosition.Left + elementWidth > containerWidth)
        newPosition.Left = containerWidth - elementWidth;

    if (newPosition.Top < 0)
        newPosition.Top = 0;
    else if (newPosition.Top + elementHeight > containerHeight)
        newPosition.Top = containerHeight - elementHeight;

    return newPosition;
}

我不确定为什么具体您的代码不适用于其他圈子。可能它与边界检查有关,如:

if (element.TranslatePoint(new Point(), container).X <= new Point(0, 0).X)
if (element.TranslatePoint(new Point(), container).X + element.RenderSize.Width >= container.RenderSize.Width)

假设new Point(0,0)TranslatePoint相对返回到每个包含网格单元格。我不确定这个假设是否正确;比较中的一个(或两个)对于应用可能是绝对的,或者是相应的。通过粗略检查您的代码很难确定;你需要运行调试器并检查你的值,看看它们与你期望的区别。