获取编译时错误“AssociatedObject”未找到

时间:2017-07-06 11:03:57

标签: c# wpf xaml

我在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();
        };
    }
}

1 个答案:

答案 0 :(得分:0)

您错过了对System.Windows.Interactivity

的引用

using System.Windows.Interactivity;放在班级的顶部。您还需要将其添加为项目中的参考。

此外,您需要修改class声明,目前是:

class class ...

应该是

public class ...