将控件设置为最顶层的 silverlight
[情况]
我有一个自定义项目控件,我将其添加到堆栈面板中,然后可以通过拖动自由移动。
public partial class CustomItem: UserControl
{
private bool IsDragging { get; set; }
private Point clickPosition;
public CustomItem()
{
InitializeComponent();
this.DataContext = this;
this.MouseLeftButtonDown += (s, ea) =>
{
clickPosition = ea.GetPosition(this.LayoutRoot);
this.CaptureMouse();
IsDragging = true;
};
this.MouseMove += (s, ea) =>
{
if (IsDragging)
{
this.transFormThisShit.X = ea.GetPosition(this).X - clickPosition.X;
this.transFormThisShit.Y = ea.GetPosition(this).Y - clickPosition.Y;
}
};
this.MouseLeftButtonUp += (s, ea) =>
{
this.ReleaseMouseCapture();
IsDragging = false;
};
}
[问题]
添加 3 个项目后,我将第一个项目拖到第二个或第三个项目上,它会跳到它后面。
当我将第二个项目拖到第一个项目上时,它会在前面,但是当我将它拖到第三个项目上时,它会在后面。
如何才能使控件拖动始终位于可视化树的顶部?
[Situation]
I have a custom item control which I add to a stack panel and after that is freely to move around by dragging.
public partial class CustomItem: UserControl
{
private bool IsDragging { get; set; }
private Point clickPosition;
public CustomItem()
{
InitializeComponent();
this.DataContext = this;
this.MouseLeftButtonDown += (s, ea) =>
{
clickPosition = ea.GetPosition(this.LayoutRoot);
this.CaptureMouse();
IsDragging = true;
};
this.MouseMove += (s, ea) =>
{
if (IsDragging)
{
this.transFormThisShit.X = ea.GetPosition(this).X - clickPosition.X;
this.transFormThisShit.Y = ea.GetPosition(this).Y - clickPosition.Y;
}
};
this.MouseLeftButtonUp += (s, ea) =>
{
this.ReleaseMouseCapture();
IsDragging = false;
};
}
[Problem]
After I add 3 items, and I drag the first item over the second or third, it jumps behind it.
When I drag the second item over the first, it goes in front but when I drag it over the third it goes behind it.
How can I make it so the control dragging is always on top of the visual tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置 ZIndex 值高于其他项目。数字越高,项目距离屏幕越“近”,因此最小的数字将位于底部。
You can set the ZIndex value of the item being dragged to be higher than the others. The higher the number, the 'closer' the item is to the screen, therefore the lowest number will be at the bottom.
当您开始拖动元素时,使用以下代码将其
Canvas.ZIndex
属性设置为大于 0 的值:并在完成拖动时清除它:
这样就可以了。
Use following code to set it's
Canvas.ZIndex
property to something greater than 0 when you start dragging your element:and clear it when you are done dragging:
That should do it.