WPF中的可拖动控件?

Ozt*_*aco 2 c# wpf xaml drag-and-drop

我对WPF还是很陌生,尽管我有一些Forms的经验,所以我决定最终尝试弄清楚如何使用WPF。因此,当我使用可拖动控件时,这是我想到的代码(我尝试将其更改为与WPF配合使用,但控件随处可见):

private void rectangle1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed) {
        double x = this.Left + (double)e.GetPosition(this).X - (double)rectangle1.Margin.Left;
        double y = this.Top + (double)e.GetPosition(this).Y - (double)rectangle1.Margin.Top;
        rectangle1.Margin = new Thickness(x, y, rectangle1.Margin.Right, rectangle1.Margin.Bottom);
    }
}
Run Code Online (Sandbox Code Playgroud)

K M*_*hta 5

您想使用装饰器来实现拖动,调整大小,旋转等操作。

  • 当涉及到拖动控件时,这确实不是一个聪明的主意,这是一个非常“重要的”任务。否则,WPF不会以装饰者的概念开始:) (4认同)