oxyplot 点击x,y交汇的点,标签一直显示

       前端:

<local:TouchPlot Grid.Row="1" x:Name="pv" local:ShowTrackerAndLeaveOpenBehavior.BindToMouseDown="Left" Margin="0,0,5,0"  />

如果你是后台写,就这样:

 TouchPlot plt = new TouchPlot { Model = GetPlot() };//GetPlot()是自定义方法
 ShowTrackerAndLeaveOpenBehavior.SetBindToMouseDown(plt, OxyMouseButton.Left);
 

public class TouchPlot : PlotView
    {
        protected override void OnTouchDown(TouchEventArgs e)
        {
            var args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
            args.RoutedEvent = PreviewMouseLeftButtonDownEvent;
            args.Source = e.OriginalSource;
            RaiseEvent(args);
            e.Handled = true;
        }
    }

public static class ShowTrackerAndLeaveOpenBehavior
    {
        public static readonly DependencyProperty BindToMouseDownProperty = DependencyProperty.RegisterAttached(
            "BindToMouseDown", typeof(OxyMouseButton), typeof(ShowTrackerAndLeaveOpenBehavior),
            new PropertyMetadata(default(OxyMouseButton), OnBindToMouseButtonChanged));

        [AttachedPropertyBrowsableForType(typeof(IPlotView))]
        public static void SetBindToMouseDown(DependencyObject element, OxyMouseButton value) =>
            element.SetValue(BindToMouseDownProperty, value);

        [AttachedPropertyBrowsableForType(typeof(IPlotView))]
        public static OxyMouseButton GetBindToMouseDown(DependencyObject element) =>
            (OxyMouseButton)element.GetValue(BindToMouseDownProperty);

        private static void OnBindToMouseButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is IPlotView plot))
                throw new InvalidOperationException($"Can only be applied to {nameof(IPlotView)}");

            //if (plot.ActualModel == null)
            //    throw new InvalidOperationException("Plot has no model");

            var controller = plot.ActualController;
            if (controller == null)
                throw new InvalidOperationException("Plot has no controller");

            if (e.OldValue is OxyMouseButton oldButton && oldButton != OxyMouseButton.None)
                controller.UnbindMouseDown(oldButton);

            var newButton = GetBindToMouseDown(d);
            if (newButton == OxyMouseButton.None)
                return;

            controller.UnbindMouseDown(newButton);
            controller.BindMouseDown(newButton, new DelegatePlotCommand<OxyMouseDownEventArgs>(
                AddStayOpenTrackerManipulator));
        }

        private static void AddStayOpenTrackerManipulator(IPlotView view, IController controller,
            OxyMouseDownEventArgs e)
        {
            controller.AddMouseManipulator(view, new StayOpenTrackerManipulator(view), e);
        }

        private class StayOpenTrackerManipulator : TrackerManipulator
        {
            private readonly PlotModel _plotModel;
            private bool _isTrackerOpen;

            public StayOpenTrackerManipulator(IPlotView plot)
                : base(plot)
            {
                _plotModel = plot?.ActualModel;

                Snap = true;
                PointsOnly = false;
            }

            public override void Started(OxyMouseEventArgs e)
            {
                if (_plotModel != null) { _plotModel.TrackerChanged += HandleTrackerChanged; }
                base.Started(e);
            }

            public override void Completed(OxyMouseEventArgs e)
            {
                if (!_isTrackerOpen)
                {
                    ReallyCompleted(e);
                }
                else
                {
                    // Completed() is called as soon as the mouse button is released.
                    // We won't call the base Completed() here since that would hide the tracker.
                    // Instead, defer the call until one of the hooked events occurs.
                    // The caller will still remove us from the list of active manipulators as soon as we return,
                    // but that's good; otherwise the tracker would continue to move around as the mouse does.
                    new DeferredCompletedCall(_plotModel, () => ReallyCompleted(e)).HookUp();
                }
            }

            private void ReallyCompleted(OxyMouseEventArgs e)
            {
                base.Completed(e);

                // Must unhook or this object will live as long as the model (instead of as long as the manipulation)
                if (_plotModel != null) { _plotModel.TrackerChanged -= HandleTrackerChanged; }
            }

            private void HandleTrackerChanged(object sender, TrackerEventArgs e) =>
                _isTrackerOpen = e.HitResult != null;

            /// <summary>
            /// Monitors events that should trigger manipulator completion and calls an injected function when they fire
            /// </summary>
            private class DeferredCompletedCall
            {
                private readonly PlotModel _plotModel;
                private readonly Action _completed;

                public DeferredCompletedCall(PlotModel plotModel, Action completed)
                {
                    _plotModel = plotModel;
                    _completed = completed;
                }

                /// <summary>
                /// Start monitoring events. Their observer lists will keep us alive until <see cref="Unhook"/> is called.
                /// </summary>
                public void HookUp()
                {
                    Unhook();

                    _plotModel.MouseDown += HandleMouseDown;
                    _plotModel.Updated += HandleUpdated;
                    _plotModel.MouseLeave += HandleMouseLeave;
                }

                /// <summary>
                /// Stop watching events. If they were the only things keeping us alive, we'll turn into garbage.
                /// </summary>
                private void Unhook()
                {
                    _plotModel.MouseDown -= HandleMouseDown;
                    _plotModel.Updated -= HandleUpdated;
                    _plotModel.MouseLeave -= HandleMouseLeave;
                }

                private void CallCompletedAndUnhookEvents()
                {
                    _completed();
                    Unhook();
                }

                private void HandleUpdated(object sender, EventArgs e) => CallCompletedAndUnhookEvents();

                private void HandleMouseLeave(object sender, OxyMouseEventArgs e) => CallCompletedAndUnhookEvents();

                private void HandleMouseDown(object sender, OxyMouseDownEventArgs e)
                {
                    CallCompletedAndUnhookEvents();

                    // Since we're not setting e.Handled to true here, this click will have its regular effect in
                    // addition to closing the tracker; e.g. it could open the tracker again at the new position.
                    // Modify this code if that's not what you want.
                }
            }
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值