标签:change 否则 attr 拖动 end obj ESS 子类 href
原文:【C#/WPF】用Thumb做可拖拽的UI控件
需求:简单的可拖拽的图片
使用System.Windows.Controls.Primitives.Thumb类
前台:
<Canvas x:Name="g">
<Thumb Canvas.Left="10" Canvas.Top="20" Canvas.ZIndex="99" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Width="60" Height="60" Source="你的图片路径"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
后台:
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
UIElement thumb = e.Source as UIElement;
// 防止Thumb控件被拖出容器。
// if (nTop <= 0)
// nTop = 0;
// if (nTop >= (g.Height - myThumb.Height))
// nTop = g.Height - myThumb.Height;
// if (nLeft <= 0)
// nLeft = 0;
// if (nLeft >= (g.Width - myThumb.Width))
// nLeft = g.Width - myThumb.Width;
// Canvas.SetTop(myThumb, nTop);
// Canvas.SetLeft(myThumb, nLeft);
// tt.Text = "Top:" + nTop.ToString() + "\nLeft:" + nLeft.ToString();
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
}
坑点:
重要的参考:
标签:change 否则 attr 拖动 end obj ESS 子类 href
原文地址:https://www.cnblogs.com/lonelyxmas/p/9473503.html