luckapple2

wpf中鼠标拖动控件移动

xaml:

<Canvas  Name="canvas2" >
            <Canvas Background="Yellow" Canvas.Left="40" Canvas.Top="20" Width="100" Name="canvas1" Height="54" >
                <Thumb Name="thumb" Height="20" DragDelta="Thumb_DragDelta" Foreground="{x:Null}" ClipToBounds="False" BorderThickness="5,2,5,0" Width="55" Canvas.Left="24" Canvas.Top="6" Background="GreenYellow"></Thumb>
            </Canvas>
        </Canvas>

cs:

private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Canvas.SetLeft(canvas1, Canvas.GetLeft(canvas1) + e.HorizontalChange);
            Canvas.SetTop(canvas1, Canvas.GetTop(canvas1) + e.VerticalChange);
        }

 

利用Thumb_DragDelta事件记录的鼠标移动距离,更改canvas的left跟top来实现的。很简单吧。

posted on 2009-11-10 15:09  luckapple2  阅读(4886)  评论(0编辑  收藏  举报

导航