moving a textbox with mouse

2019-08-23 07:08发布

问题:

I have a custom WPF control which consist of single TextBox

<UserControl HorizontalAlignment="Left" x:Class="WPFDiagramDesignerControl.Components.UcWBSBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="100" IsEnabled="True">

    <Grid >
        <TextBox x:Name="txtBox" IsEnabled="True"   Background="AntiqueWhite"  Margin="10,10,10,10" TextWrapping="Wrap"> </TextBox>
    </Grid>

</UserControl>

The control is placed on Canvas called MyDesigner.

I want to move my control on canvas only if I click on textbox and move mouse. I don't want to move control when I click on margin between borders of control and textbox. I started writting a code and it looks like that

public partial class UcWBSBlock : UserControl
{

    bool textChanged = false;
    bool isClicked = false;
    Point startPoint;
    DesignerItem parentItem;
    DesignerCanvas parentCanvas;

    public UcWBSBlock()
    {

        InitializeComponent();

        txtBox.MouseDoubleClick+=new MouseButtonEventHandler(txtBox_MouseDoubleClick);
        txtBox.MouseMove+=new MouseEventHandler(txtBox_MouseMove);
        txtBox.PreviewMouseDown+=new MouseButtonEventHandler(txtBox_PreviewMouseDown);
        txtBox.PreviewMouseUp+=new MouseButtonEventHandler(txtBox_PreviewMouseUp);
        txtBox.Cursor = Cursors.SizeAll;

    }

    private void txtBox_MouseMove(object sender, RoutedEventArgs e)
    {
        if (isClicked)
        {
            Point mousePos = Mouse.GetPosition(parentCanvas);

            parentItem = this.Parent as DesignerItem;
            parentCanvas = parentItem.Parent as DesignerCanvas;
            Point relativePosition = Mouse.GetPosition(parentCanvas);
            DesignerCanvas.SetLeft(parentItem,DesignerCanvas.GetLeft(parentItem) -  (startPoint.X - mousePos.X));
            DesignerCanvas.SetTop(parentItem, DesignerCanvas.GetTop(parentItem) - (startPoint.Y - mousePos.Y));
        }  
    }

    private void txtBox_PreviewMouseDown(object sender, RoutedEventArgs e)
    {
        if (!isClicked)
        {
            isClicked = true;
            parentItem = this.Parent as DesignerItem;
            parentCanvas = parentItem.Parent as DesignerCanvas;
            startPoint = Mouse.GetPosition(parentCanvas);
        }
    }

    private void txtBox_PreviewMouseUp(object sender, RoutedEventArgs e)
    {
        isClicked = false;
    }
}

}

However my control doesn't move :( What did I do wrong ?? It's hard to debug this :)

回答1:

you are setting the left/top of the parent item, not your control:

DesignerCanvas.SetLeft(parentItem,DesignerCanvas.GetLeft(parentItem) -  (startPoint.X - mousePos.X));
DesignerCanvas.SetTop(parentItem, DesignerCanvas.GetTop(parentItem) - (startPoint.Y - mousePos.Y));

should (probably) be like this:

DesignerCanvas.SetLeft(this,DesignerCanvas.GetLeft(this) -  (startPoint.X - mousePos.X));
DesignerCanvas.SetTop(this, DesignerCanvas.GetTop(this) - (startPoint.Y - mousePos.Y));


标签: c# wpf controls