【问题标题】:Tooltip position in OxyplotOxyplot 中的工具提示位置
【发布时间】:2019-06-11 20:42:41
【问题描述】:

我最近使用 OxyPlot 为我的 WPF 项目绘制图表。我对系列中的工具提示有疑问。工具提示的矩形框离我的鼠标定位的点太近。我想知道是否有任何方法可以修改工具提示框的位置并使其远离光标。非常感谢。例如,我想将提示框(红色框内)向右移动一点。

[编辑]

按照 Anu 的想法,我添加了以下代码,但似乎无法按预期工作。

在 xaml.cs 文件中,我添加了

<UserControl.Resources>
    <popups:ScreenPointToOffSetScreenPointConverter x:Key="ScreenPointToOffSetScreenPointConverter"/>
</UserControl.Resources>

然后将我的 OxyPlot 更改为

<ItemsControl ItemsSource="{Binding AmplitudeDtos}" Margin="0" Name="Amplitude" >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid popups:GridUtilities.ItemsPerRow="1" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type dtoModels:SignalDto}">
            <oxy:PlotView Height="Auto" Width="Auto" MinHeight="40" MinWidth="100" HorizontalContentAlignment="Stretch" 
                          FontStyle="Normal" FontFamily="Roboto, Microsoft YaHei" FontSize="8" Model="{Binding PlotModel}" Controller="{Binding PlotController}" Padding="8" Margin="0">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <i:InvokeCommandAction Command="{Binding SignalLoadedCommand}" CommandParameter="{Binding}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <oxy:PlotView.DefaultTrackerTemplate>
                    <ControlTemplate>
                        <oxy:TrackerControl Position="{Binding Position, Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
                            <oxy:TrackerControl.Content>
                                <TextBlock Text="{Binding}" />
                            </oxy:TrackerControl.Content>
                        </oxy:TrackerControl>
                    </ControlTemplate>
                </oxy:PlotView.DefaultTrackerTemplate>
            </oxy:PlotView>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>    

请注意,我的 OxyPlot 位于 DataTemplate 中。

ScreenPointToOffSetScreenPointConverter 类与 Anu 的示例完全相同。

【问题讨论】:

    标签: oxyplot


    【解决方案1】:

    一个相对简单的解决要求是修改 DefaultTrackerTemplate 并使用自定义转换器来更改位置。例如,您可以定义一个 ScreenPoint 转换器如下

    public class ScreenPointToOffSetScreenPointConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if(value is ScreenPoint screenPoint)
                {
                    return new ScreenPoint(screenPoint.X + 50, screenPoint.Y);
                }
                return value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                // TODO: Implement
                throw new NotImplementedException();
            }
        }
    

    然后在你的 XAML 中

    <oxy:PlotView  Model="{Binding MyModel}">
                <oxy:PlotView.DefaultTrackerTemplate>
                    <ControlTemplate>
                        <oxy:TrackerControl Position="{Binding Position,Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
                            <TextBlock Text="{Binding}" />
                        </oxy:TrackerControl>
                    </ControlTemplate>
                </oxy:PlotView.DefaultTrackerTemplate>
            </oxy:PlotView>
    

    转换器将为跟踪器的 ScreenPoint 位置添加一个偏移量。如果对您的应用程序要求有意义,您可以修改转换器的偏移值。

    【讨论】:

    • 嗨,阿努,谢谢你的建议。我按照您的想法添加了代码。没有明显的错误,但似乎没有按预期工作。你能帮忙看看有什么问题吗?
    • @Ben 我检查了您的代码,无法查看或复制它为什么不起作用。可能您可以通过首先将图形与项目控件隔离来开始调试。检查它是否正常工作,然后添加项目控件。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多