SciChart:从代码绑定的多个 DataSeries 的自定义 RolloverModifierLabel

SciChart: Custom RolloverModifierLabel for multiple DataSeries bound from code

在我更新到最新版本的 SciChart 之前,我有这个自定义翻转修改器,它可以显示任何给定点的多个值 "rolled over"。 它是这样实现的:

<sci:RolloverModifier
     DrawVerticalLine="True"
     ShowTooltipOn="Always"
     SourceMode="AllVisibleSeries"
     TooltipLabelTemplate="{StaticResource RolloverLabelTemplate}" />

RolloverLabelTemplate 是一个 ControlTemplate:

 <ControlTemplate
     x:Key="RolloverLabelTemplate"
     TargetType="sci:TemplatableControl">
     <Grid>
     ...

现在 RolloverModifier.TooltipLabelTemplate 已从 API 中消失,似乎被 TooltipTemplate 取代,它采用 DataTemplate,而不是 ControlTemplate。我尝试制作一个类似的 DataTemplate:

<DataTemplate
 x:Key="SomeTemplate"
 DataType="s:XySeriesInfo">
<Grid>

但是当我尝试将它分配给 RolloverModifier 时,

 <s:RolloverModifier
    ...
    TooltipTemplate="{StaticResource SomeTemplate}" />

我得到以下异常:

Unable to cast object of type 'SciChart.Charting.ChartModifiers.RolloverModifier' to type 'SciChart.Charting.Visuals.RenderableSeries.BaseRenderableSeries'.

我已尝试遵循此文档:https://www.scichart.com/documentation/v4.x/webframe.html#RolloverModifier.html

关于设置工具提示模板样式的主题,它建议使用 RolloverModifier,但要将 TooltipTemplate 添加到 RenderableSeries:

<s:SciChartSurface.RenderableSeries>
      <s:FastLineRenderableSeries s:RolloverModifier.TooltipTemplate="{StaticResource XyTooltipTemplate}"/>                              
   </s:SciChartSurface.RenderableSeries>           

   <s:SciChartSurface.ChartModifier>
      <s:ModifierGroup>
         <s:RolloverModifier ShowTooltipOn="Always" />                   
      </s:ModifierGroup>
   </s:SciChartSurface.ChartModifier>
</s:SciChartSurface>

这对我来说是个问题,因为我没有在 xaml 中定义的 RenderableSeries。它们绑定到视图模型:

 <sciVisuals:SciChartSurface
   ...
   SeriesSource="{Binding SciSeries}">

会有不止一个,实际上我什至不知道有多少。在这种情况下如何自定义鼠标悬停工具提示标签?

在 SciChart WPF v4.x 或更高版本中,有必要将 TooltipTemplate 应用于系列,因为许多用户要求能够单独设置系列样式。例如,Candlestick 系列应该具有与 Line 系列不同的工具提示。

请参阅 v4+ 中的 SciChart WPF Documentation for applying a RolloverModifier TooltipTemplate:

Styling the Tooltip Item Template

SciChart by default has a number of Tooltip templates which are unique to the series type.
To change the Tooltip template, use the RolloverModifier.TooltipTemplate attached property:

<s:SciChartSurface >
   <s:SciChartSurface.Resources>
      <!-- Tooltip Template for an XyDataSeries binds to XySeriesInfo -->
      <!-- Check out the properties on XySeriesInfo to see what you can bind to -->
      <DataTemplate x:Key="XyTooltipTemplate" DataType="s:XySeriesInfo">
         <StackPanel Orientation="Vertical">              
            <TextBlock Foreground="White">
               <Run Text="Series: "/>
               <Run Text="{Binding SeriesName, StringFormat='{}{0}:'}"/>
            </TextBlock>              
            <TextBlock Foreground="White">
               <Run Text="X-Value: "/>
               <Run Text="{Binding FormattedXValue}"/>
            </TextBlock>
            <TextBlock Foreground="White">
               <Run Text="Y-Value: "/>
               <Run Text="{Binding FormattedYValue}"/>
            </TextBlock>
         </StackPanel>
      </DataTemplate>
   </s:SciChartSurface.Resources>
   <s:SciChartSurface.RenderableSeries>
      <s:FastLineRenderableSeries s:RolloverModifier.TooltipTemplate="{StaticResource XyTooltipTemplate}"/>                              
   </s:SciChartSurface.RenderableSeries>           
   <s:SciChartSurface.ChartModifier>
      <s:ModifierGroup>
         <s:RolloverModifier ShowTooltipOn="Always" />                   
      </s:ModifierGroup>
   </s:SciChartSurface.ChartModifier>
</s:SciChartSurface>

尽管使用了 MVVM API,但为了设置样式,我建议在 XAML 中创建样式并使用 SeriesBinding MVVM API 应用它们。

how to style a tooltip when using the MVVM API here 上的 SciChart WPF 文档中有一个常见问题解答。

A lot of customers ask us how to style various attached properties used in SciChart in the MVVM API. For example, the given following attached properties, how do we convert this to the MVVM API?

The solution is simple, to use our method of Styling the RenderableSeries presented in Worked Example - Style a Series in MVVM.

Simply declare the attached properties and tooltip templates in a style in your View.

Now, apply the style using BaseRenderableSeriesViewModel.StyleKey property. SciChart will pick up the style and do the rest.

如果您仍在使用较旧的 SeriesSource API(请注意,从 v4.x 开始,这已经过时了)那么我建议在 XAML 中声明您的样式并使用 this technique to access a style in code-behind