[英]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 輸出窗口中是否有任何警告。 在控制台/輸出窗口中查找這樣的行
System.Windows.Data 信息:10:無法使用綁定檢索值並且不存在有效的回退值; 改為使用默認值。 BindingExpression:Path=IDontExist; 數據項=空; 目標元素是“TextBlock”(名稱=”); 目標屬性是“文本”(類型“字符串”)
如果輸出窗口中沒有 BindingExpression 錯誤,接下來要檢查的是綁定失敗的對象的 DataContext。 它是什么類型? 它是在運行時設置的嗎?
WPF Snoop是檢查綁定的一個很好的工具。 您可以將鼠標懸停在項目上並實時查看屬性。 您還可以在那里看到綁定錯誤。 這里有一個相關的問題和答案,它顯示了如何做到這一點。
如果存在綁定錯誤,則將顯示為紅色,您可以在運行時檢查 DataContext 是否為 null 或錯誤類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.