We are using the VerticalLineAnnotation and want to change it’s location (X1 property) in the viewmodel.
Annotations are bound like this:
<s:SciChartSurface
...
Annotations="{s:AnnotationsBinding Annotations}"
to the property:
public ObservableCollection<IAnnotationViewModel> Annotations { get; private set; }
The collection contains this association:
this.nowAnnotation = new VerticalLineAnnotationViewModelEx()
{
X1 = TimeSpan.FromSeconds(0),
StyleKey = "NowAnnotationStyle"
};
To change the location we change the value of X1:
nowAnnotation.X1 = value;
Calling that didn’t have a direct effect on the UI unless the user does any action which redraws the chart.
We created a style which sets an attached property to pass the VerticalLineAnnotation to the view model, this works fine.
<Style TargetType="s:VerticalLineAnnotation" x:Key="NowAnnotationStyle">
<Setter Property="viewModels:VerticalLineAnnotationViewModelEx.PassIAnnotation" Value="True" />
</Style>
And we added a Refresh method to VerticalLineAnnotationViewModelEx and call it after setting X1:
nowAnnotation.X1 = value;
nowAnnotation.Refresh();
The first implementation of the Refresh method we did was:
public void Refresh()
{
this.Annotation.Refresh();
}
From the documentation this should redraw the annotation without redrawing the whole chart, but it had no effect.
https://www.scichart.com/documentation/win/current/webframe.html#SciChart.Charting~SciChart.Charting.Visuals.Annotations.AnnotationBase~Refresh.html
It only started working after changing to this:
public void Refresh()
{
this.Annotation.ParentSurface.InvalidateElement();
}
Is there a better way to do it, shouldn’t the first implementation work?
The complete code of VerticalLineAnnotationViewModelEx
public class VerticalLineAnnotationViewModelEx : VerticalLineAnnotationViewModel
{
public static readonly DependencyProperty PassIAnnotationProperty = DependencyProperty.RegisterAttached(
"PassIAnnotation", typeof(bool), typeof(VerticalLineAnnotationViewModelEx),
new PropertyMetadata(default(bool), OnPassIAnnotationChanged));
public IAnnotation Annotation { get; private set; }
private static void OnPassIAnnotationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var verticalLineAnnotation = (VerticalLineAnnotation)d;
((VerticalLineAnnotationViewModelEx)verticalLineAnnotation.DataContext).Annotation = (IAnnotation)d;
}
public static void SetPassIAnnotation(DependencyObject element, bool value)
{
element.SetValue(PassIAnnotationProperty, value);
}
public static bool GetPassIAnnotation(DependencyObject element)
{
return (bool)element.GetValue(PassIAnnotationProperty);
}
public void Refresh()
{
//this.Annotation?.Refresh();
this.Annotation?.ParentSurface.InvalidateElement();
}
}
- AC42admin LMB asked 1 year ago
- last edited 1 year ago
-
Hi there, code formatting on the forum requires pressing the Code button or alternatively, indenting four spaces. Can you reformat the code sample?
-
done
-
Thanks – will pass to the team. This should work out of the box. I wonder without VerticalLineAnnotationViewModelEx (using the standard annotation view model) does it work? If YES then the answer is probably in how styles where the default base class bindings are applied. For example in this documentation page https://www.scichart.com/documentation/win/current/webframe.html#Worked%20Example%20-%20CustomRenderableSeries%20in%20MVVM.html notice how the Style for the custom series has BasedOn (Series, Annotations, Axis in MVVM all have the same schema and require similar things to setup for bindings to custom types)
- You must login to post comments
Hi there,
I’ve checked our examples and can confirm that the X1 annotation binding works properly out of the box. There seems to be a problem with your custom annotation style. When you create a custom style for an MVVM annotation, you should base it on the corresponding SciChart style to inherit the bindings.
<Style x:Key="AnnotationExtendedStyle"
BasedOn="{StaticResource {x:Type s:VerticalLineAnnotationForMvvm}}"
TargetType="s:VerticalLineAnnotationForMvvm">
<Setter Property="Stroke" Value="DodgerBlue"/>
<Setter Property="StrokeThickness" Value="2"/>
</Style>
The attached example shows how to create a custom MVVM annotation. Please take a look and let us know if everything is working right from your side.
Best regards,
Demien B.
SciChart Software Engineer
- Demien B. answered 1 year ago
- You must login to post comments
Thanks for pointing out that it should work with the VerticalLineAnnotationViewModel without the AttachedProperty. The only reason I needed the VerticalLineAnnotationViewModelEx was the annotation to pass the Annotation dependency object.
So I reverted back to VerticalLineAnnotationViewModel and eventually figured out that the problem was YAxis Id. Modifying the test project to
<s:SciChartSurface.YAxis>
<s:NumericAxis Id="1" />
</s:SciChartSurface.YAxis>
stops the annotation from moving as long as the surface is not redrawn. Setting the YAxisId on the annotation fixes it.
var annotationEx = new VerticalLineAnnotationViewModel { YAxisId = “1” }
Thank you for the demo project. The style wasn’t actually my issue as I’ve started
- AC42admin LMB answered 1 year ago
- last edited 1 year ago
- You must login to post comments
Please login first to submit.