c# - 如何像在 Windows 中拖动应用程序窗口一样拖动堆栈面板(而不是拖放)
问题描述
我正在制作一个uwp App,它是一个小部件应用程序,如果用户想要重新定位小部件,(每个小部件都是一个带有内容的堆栈面板),堆栈面板应该根据鼠标移动,就像在windows中拖动应用程序窗口一样,请帮助我,我一直在尝试,但一无所获!
解决方案
要使 StackPanel 可移动,您必须向其中添加这两个事件。
PointerPressed="StackPanel_PointerPressed"
PointerMoved="StackPanel_PointerMoved"
在您的 C# 代码中,您必须创建一个变量并添加一个 using:
using Windows.Foundation;
private Point MouseDownLocation;
StackPanel_PointerPressed 事件:
private void StackPanel_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender is StackPanel sp)
{
if (e.GetCurrentPoint(sp).Properties.IsLeftButtonPressed)
{
MouseDownLocation.Y = e.GetCurrentPoint(sp).Position.Y;
MouseDownLocation.X = e.GetCurrentPoint(sp).Position.X;
}
}
}
以及 StackPanel_PointerMoved 事件:
private void StackPanel_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (sender is StackPanel sp)
{
if (e.GetCurrentPoint(sp).Properties.IsLeftButtonPressed)
{
var MarginLeft = e.GetCurrentPoint(sp).Position.X + sp.Margin.Left - MouseDownLocation.X;
var MarginTop = e.GetCurrentPoint(sp).Position.Y + sp.Margin.Top - MouseDownLocation.Y;
sp.Margin = new Thickness(MarginLeft, MarginTop, sp.Margin.Right, sp.Margin.Bottom);
}
}
}
推荐阅读
- sql - 打印错误并继续while循环
- android - SparseArray 错误:调用需要 API 级别 S
- java - JacksonNode迭代器,获取键值
- node.js - Mongoose EJS SubCategory - 如何查看嵌套类别
- reactjs - 下一个图像组件不加载图像
- python - 刷新页面并在未找到时再次尝试 find_element 的简单条件
- templates - 错误:无法识别“apps.yml”:没有匹配 /,Kind=Template
- systemtap - polkit-0.115-11.el8.x86_64 的调试信息
- mysql - Model::Create() 方法没有插入 InnoDB 存储引擎
- java - 无法使用 Scala/Java 中的模式从日期创建时间戳