【问题标题】:OxyPlot: Customize TooltipOxyPlot:自定义工具提示
【发布时间】:2020-07-17 08:20:32
【问题描述】:

我在工作中使用 OxyPlot 来显示一些信息。而且我需要更改默认工具提示,您可以在单击图表中的某个点后看到它。

目前,我有一个带有简单线性系列的测试 WPF 窗口。我已更改工具提示的模板以显示一些文本和按钮。

我的控制器:

public class PlotViewTest : PlotView
{ }

public class TestTracker : TrackerControl
{
    public TestTracker()
    {
        CanCenterHorizontally = false;
        CanCenterVertically = false;
    }
}

我的 WPF 窗口代码:

<controlers:PlotViewTest Model="{Binding MyModel}">
    <controlers:PlotViewTest.DefaultTrackerTemplate>
        <ControlTemplate>
            <controlers:TestTracker Position="{Binding Position}">
                <Grid Margin="15">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Margin="5" Text="Hello world!"/>
                    <Button Grid.Row="1" Margin="5" Content="Start"/>
                </Grid>
            </controlers:TestTracker>
        </ControlTemplate>
    </controlers:PlotViewTest.DefaultTrackerTemplate>
</controlers:PlotViewTest>

我的 WPF 窗口:

但我想改变一些行为。

  1. 只有按住鼠标左键才能看到工具提示。当我打开它时,工具提示将被隐藏。但是我需要在红点上单击鼠标左键 1 次后始终显示工具提示。当我单击第二个红点时,将显示另一个工具提示。但是当我点击一些空白区域时,工具提示应该被隐藏。
  2. 当我在蓝线上按住鼠标左键时,也可以显示工具提示。但我想阻止它。只有当我点击红点时才会显示它。

如何改变这两种行为??

【问题讨论】:

    标签: wpf oxyplot


    【解决方案1】:

    您可以通过编写覆盖跟踪器的Completed 操作的自定义跟踪器操纵器来实现目标。例如

    public class StaysOpenTrackerManipulator : TrackerManipulator
    {
            public StaysOpenTrackerManipulator(IPlotView plotView) : base(plotView)
            {
                Snap = true;
                PointsOnly = true;
            }
            public override void Completed(OxyMouseEventArgs e)
            {
                // Do nothing
            }
    }
    

    通过将SnapPointsOnly 属性设置为true,您可以确保仅在选择点而不是其他位置(线/外部)时打开跟踪器。

    您可以使用 PlotController 将自定义 TrackerManipulator 绑定到 PlotView。

    // Property
    public PlotController CustomPlotController { get; set; }
    
    // Assign Value for CustomPlotController 
    var customController = new PlotController();
    customController.UnbindAll();
    customController.BindMouseDown(OxyMouseButton.Left, new DelegatePlotCommand<OxyMouseDownEventArgs>((view, controller, args) =>
                    controller.AddMouseManipulator(view, new StaysOpenTrackerManipulator(view), args)));
    CustomPlotController = customController;
    

    在 Xaml 中

    <controlers:PlotViewTest Model="{Binding MyModel}" Controller="{Binding CustomPlotController}">
    

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多