在网格中拖动文本框

本文关键字:文本 拖动 网格 | 更新日期: 2023-09-27 17:49:33

我想问如何移动一个文本框,这是一个网格的孩子。这段代码改变了文本框的左侧,但没有移动它。有没有办法设置父文本框的画布。提前感谢您的回复。

private void Handle_Drag(object sender, ManipulationDeltaEventArgs e)
{
    TextBox Text = sender as TextBox;
    Text.Text = "I'm moved";
    double currentX = Canvas.GetLeft(Text);
    double currentY = Canvas.GetTop(Text);
    MessageBox.Show(currentX.ToString());
    MessageBox.Show(currentY.ToString()); 
    Canvas.SetLeft(Text, currentX + e.DeltaManipulation.Translation.X);
    Canvas.SetTop(Text, currentY + e.DeltaManipulation.Translation.Y); 
} 

在网格中拖动文本框

尝试查找父文本框并将其从该网格中删除。

   private void Handle_Drag(object sender,System.Windows.Input.ManipulationDeltaEventArgs e)
    {
        TextBox Text = sender as TextBox;
        var parent = Text.Parent as Grid;
        if (parent  != null)
        {
            parent.Children.Remove(Text);
            myCanvas.Children.Add(Text);
        }
        Text.Text = "I'm moved";
        double currentX = Canvas.GetLeft(Text);
        double currentY = Canvas.GetTop(Text);
        MessageBox.Show(currentX.ToString());
        MessageBox.Show(currentY.ToString());
        Canvas.SetLeft(Text, currentX + e.DeltaManipulation.Translation.X);
        Canvas.SetTop(Text, currentY + e.DeltaManipulation.Translation.Y); 
    }

或者你可以使用交互行为API来拖动。http://developer.nokia.com/community/wiki/Drag_%26_Drop_in_Windows_Phone

首先你必须将TextBox的parent设置为null

Text.Parent = null;

然后添加TextBox作为所需画布的子元素

desiredCanvas.Children.Add(Text);

(假设画布名称为name ="desiredCanvas")