我在Wpf中实现了Popup控件。 我想让弹出式拖动,并找到下面写的一些代码。 但我得到编译时错误“AssociatedObject”未找到,行为未找到。 这是我的代码:
class class MouseDragPopupBehavior : Behavior<Popup>
{
private bool mouseDown;
private Point oldMousePosition;
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += (s, e) =>
{
mouseDown = true;
oldMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
AssociatedObject.Child.CaptureMouse();
};
AssociatedObject.MouseMove += (s, e) =>
{
if (!mouseDown) return;
var newMousePosition = AssociatedObject.PointToScreen(e.GetPosition(AssociatedObject));
var offset = newMousePosition - oldMousePosition;
oldMousePosition = newMousePosition;
AssociatedObject.HorizontalOffset += offset.X;
AssociatedObject.VerticalOffset += offset.Y;
};
AssociatedObject.MouseLeftButtonUp += (s, e) =>
{
mouseDown = false;
AssociatedObject.Child.ReleaseMouseCapture();
};
}
}
答案 0 :(得分:0)
您错过了对System.Windows.Interactivity
将using System.Windows.Interactivity;
放在班级的顶部。您还需要将其添加为项目中的参考。
此外,您需要修改class
声明,目前是:
class class ...
应该是
public class ...