【发布时间】:2020-06-29 14:57:21
【问题描述】:
我创建了一个复合注释(名为 PeakAnnotation),它由四个元素组成:两个 VerticalLineAnnotation、一个 BoxAnnotation 和一个 TextAnnotation。当我第一次添加注释时,一切都正确显示。但是,当我在 TabControl 中的选项卡之间切换时...BoxAnnotation 消失了。当我用光标拖动复合注释几个像素时,该框将重新出现。
我已经尝试调用ZoomExtents() 和InvalidateElement(),但没有解决问题。
我创建了一个简单的应用程序,以最简单的方式重现问题。
PeakAnnotation.xaml
<s:CompositeAnnotation x:Class="WpfPresentation.Views.PeakAnnotation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
Canvas.ZIndex="1"
DragDirections="XDirection"
ResizeDirections="XDirection"
IsEditable="True">
<s:CompositeAnnotation.Annotations>
<s:VerticalLineAnnotation CoordinateMode="Relative" Stroke="#FFBADAFF" StrokeThickness="2" X1="0" X2="0" Y1="0" Y2="1"/>
<s:VerticalLineAnnotation CoordinateMode="Relative" Stroke="#FFBADAFF" StrokeThickness="2" X1="1" X2="1" Y1="0" Y2="1"/>
<s:BoxAnnotation x:Name="box" Opacity="0.2" CornerRadius="2" Background="#FFBADAFF" BorderBrush="#1964FF" CoordinateMode="Relative" X1="0" X2="1" Y1="0" Y2="1"/>
<s:TextAnnotation x:Name="AnnotationTextLabel" CoordinateMode="Relative" X1="0" Y1="0.95" FontSize="12" Foreground="White"/>
</s:CompositeAnnotation.Annotations>
</s:CompositeAnnotation>
PeakAnnotation.xaml.cs
public partial class PeakAnnotation : CompositeAnnotation
{
public PeakAnnotation()
{
}
public PeakAnnotation(string annotationText)
{
InitializeComponent();
AnnotationTextLabel.Text = annotationText;
}
public string StyleKey { get; set; }
public Type ViewType => throw new NotImplementedException();
}
MainViewModel.cs
public MainViewModel()
{
ChartTitle = "Testing";
Annotations = new AnnotationCollection();
var myAnnotation = new PeakAnnotation("My Annotation Title")
{
X1 = 40,
X2 = 50,
Y1 = 0,
Y2 = 100
};
Annotations.Add(myAnnotation);
}
public string ChartTitle { get; set; }
public AnnotationCollection Annotations { get; set; }
}
MainWindow.xaml
<Window x:Class="SciChartTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SciChartTesting" xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<TabControl>
<TabItem Header="TabOne">
<Label Content="This is TabOne"/>
</TabItem>
<TabItem Header="TabTwo">
<s:SciChartSurface ChartTitle="{Binding ChartTitle}" Annotations="{Binding Annotations}">
<s:SciChartSurface.XAxis>
<s:NumericAxis VisibleRange="0,100"/>
</s:SciChartSurface.XAxis>
<s:SciChartSurface.YAxis>
<s:NumericAxis VisibleRange="0,100"/>
</s:SciChartSurface.YAxis>
</s:SciChartSurface>
</TabItem>
</TabControl>
</Grid>
【问题讨论】:
-
TabControl只使用一个共享的ContentPresenter来显示TabItem的内容。这意味着它会删除选项卡导航之间的内容。不保留装饰层等渲染细节。您必须在内容加载时显式重绘注释元素,例如使用触发器。 -
或者您可以创建自己的选项卡控件,它只隐藏不应该显示的元素
-
@BionicCode 如果您想要赏金。发布您的消息作为答案。解决方案似乎是在更改选项卡时重新创建注释元素。
-
好的。谢谢你。是的,这是正确的解决方案。您必须重新绘制有状态的视觉效果,例如验证错误模板的修饰符等。这是一些意想不到的行为。但这很有意义。