在 WPF 中进行拖放时显示鼠标点
我正在使用 Lesters DragAndDropManager 为了在我的应用程序中获得拖放功能,我真的很喜欢它的实现方式,但是我有一个小问题,那就是我想在状态栏中拖动期间显示鼠标协调,那么如何发送鼠标位置从 DropManager 到我的 xaml 代码。
我尝试在管理器中添加一个可以在 xaml 代码中绑定的依赖属性。
public static readonly DependencyProperty MousePointProperty =
DependencyProperty.RegisterAttached("MousePoint", typeof(Point), typeof(DragDropBehavior),
new FrameworkPropertyMetadata(default(Point)));
public static void SetMousePoint(DependencyObject depObj, bool isSet)
{
depObj.SetValue(MousePointProperty, isSet);
}
public static IDragSourceAdvisor GetMousePoint(DependencyObject depObj)
{
return depObj.GetValue(MousePointProperty) as IDragSourceAdvisor;
}
在 Xaml 中,我像这样绑定它。
<StatusBar>
<TextBlock Text="{Binding local:DragDropBehavior.MousePoint.X}"/>
</StatusBar>
但是如何将鼠标协调设置为管理器中我的依赖属性呢?
private static void DropTarget_PreviewDragOver(object sender, DragEventArgs e)
{
if (UpdateEffects(sender, e) == false) return;
//-- Update position of the preview Adorner
Point position = GetMousePosition(sender as UIElement);
//-- Here I Want to do this, but that not posible because the SetMousePoint takes a dependencyObject and not my value.
//-- SetMousePoint(position);
_draggedUIElementAdorner.Left = position.X - _offsetPoint.X;
_draggedUIElementAdorner.Top = position.Y - _offsetPoint.Y;
e.Handled = true;
}
我认为我在这里错了,但我一直卡在如何通过绑定到 DragAndDropManager 来将鼠标协调到 xaml 代码上。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确切地。您无法以您想要的方式绑定到附加属性,因为您必须知道它所附加的对象。
如何做到这一点?我看到三个选项(但还有更多)。
DragAndDropManager
公开静态事件,在自定义状态栏类中订阅它。每当发生拖动时,都会从DragAndDropManager
触发事件。但要小心静态事件。很容易引入内存泄漏...将
DragAndDropManager
转换为单例。例如,在其中实现 INotifyValueChanged,创建实例属性MousePoint
。从状态栏绑定到它:Text="{Binding MousePoint.X, Source={x:Static local:DragDropBehavior.Instance}}"
每当发生拖动时,更新实例属性,并引发属性更改事件。
希望这有帮助,
干杯,Anvaka
Exactly. You cannot bind to attached property, in a way you want, because you have to know an object where it's attached.
How to do this? I see three options (but there are many more).
DragAndDropManager
, subscribe to it in the custom status bar class. Whenever drag occurs, fire event fromDragAndDropManager
. But be careful with static events. It's very easy to introduce a memory leak...Convert
DragAndDropManager
into singleton. Implement INotifyValueChanged in it, create instance propertyMousePoint
, for say. Bind to it from statusbar:Text="{Binding MousePoint.X, Source={x:Static local:DragDropBehavior.Instance}}"
Whenever drag occurs, update instance property, and raise property changed event.
Hope this helps,
Cheers, Anvaka