-1
我在Wpf中實現了Popup控件。 我想做出彈出式拖拽功能,並找到下面寫的一些代碼。 但我得到編譯時錯誤「AssociatedObject」未找到,行爲未找到。 這裏是我的代碼:Geeting編譯時間錯誤
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();
};
}
}
那麼什麼是'AssociatedObject'應該是再?這裏沒有定義。 – DavidG
你確定這是確切的源代碼?開始時的「班級」似乎很奇怪。 – mjwills
是的,其實我從這個鏈接得到了這段代碼:https://stackoverflow.com/questions/4784240/a-draggable-popup-control-in-wpf –