SciChart(C# WPF)。在 TextAnnotation 中绑定文本 属性 时出现问题

SciChart (C# WPF). Problem with binding Text property in TextAnnotation

我有一个简单的 SciChart CompositeAnnotation:

 <s:CompositeAnnotation x:Class="KernelDensity.DensityAnnotation"
         xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Canvas.ZIndex="1" MouseDoubleClick="DensityAnnotation_OnMouseDoubleClick">
<s:CompositeAnnotation.Annotations>
    <s:BoxAnnotation x:Name="DensityBoxArea" Background="#FFBADAFF" BorderBrush ="Black" BorderThickness="1" CoordinateMode="Relative" CornerRadius="1" Width="1" Height="1" Opacity="0.5" X1="0" X2="1" Y1="0" Y2="1" >
        <s:BoxAnnotation.Template>
            <ControlTemplate TargetType="s:BoxAnnotation">
                <Border x:Name="PART_BoxAnnotationRoot"
                        Margin="{TemplateBinding Margin}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="{TemplateBinding CornerRadius}">
                    <Image x:Name="DensityPlot" Width="Auto" Stretch="Fill"/>
                </Border>
            </ControlTemplate>
        </s:BoxAnnotation.Template>
    </s:BoxAnnotation>
    <s:TextAnnotation x:Name="SelectedSeriesTextAnnotation"
                      Background="Black"
                      CoordinateMode="Relative"
                      CornerRadius="3"
                      Foreground="White"
                      HorizontalAnchorPoint="Center"
                      X1="0.5"
                      Y1="0.5"
                      FontSize="14"
                      Text="{Binding SelectedSeriesTextAnnotationText}">
    </s:TextAnnotation>
</s:CompositeAnnotation.Annotations>
<s:CompositeAnnotation.ToolTip >
    <ToolTip Placement="Right">
        <TextBlock Text="{Binding SelectedSeriesTextAnnotationText}" FontSize="15" Foreground="Black"/>
    </ToolTip>
</s:CompositeAnnotation.ToolTip>
</s:CompositeAnnotation>

在这里您可以看到 TextAnnotation-Text 属性和 ToolTip-TextBlock-Text 属性对我的视图模型具有相同的绑定 属性。问题是此绑定适用于 ToolTip TextBlock Text 属性,但不适用于 TextAnnotation Text 属性(就好像根本没有绑定一样)。 我该如何解决这个问题?

诊断任何绑定问题时首先要检查的是 Visual Studio 输出 window 中是否有任何警告。在控制台/输出中查找类似这样的行 window

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=IDontExist; DataItem=null; target element is ‘TextBlock’ (Name=”); target property is ‘Text’ (type ‘String’)

如果输出中没有 BindingExpression 错误 window,接下来要检查的是未能绑定的对象的 DataContext。它是什么类型的?是在运行时设置的吗?

检查绑定的一个很好的工具是 WPF Snoop. You can hover an item and see the properties live. You can also see binding errors right there. There is a related question and answer here,它展示了如何做到这一点。

如果有绑定错误,将显示为红色,您可以在运行时检查 DataContext 是否为空或类型错误。