Pre loader

Change Annotation location and refresh in MVVM project.

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1
0

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();
    }
}
Version
7.0.2.27161
  • You must to post comments
1
0

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

Attachments
  • You must to post comments
1
0

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

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.