Hi I am trying to create a vertical line annotation with MVVM annotationsbinding – this is due to having a chart group with 2 SciChartSurfaces sharing one X axis.
I have added a VerticalLineAnnotationViewModel to my
ObservableCollection Annotations
I would like the user to be able to create a vertical line with two labels, one with the X1 position and the other with some custom text.
I have tried adding an ObservableCollection AnnotationLabels to the VerticalLineAnnotationViewModel during instantiation however they don’t seem to appear.
As an alternative I tried a CustomAnnotation called CustomVerticalLine which inherited CustomAnnotation and then created a CustomVerticalLineViewModel which inherited CustomAnnotationViewModel and then overrode the ViewType to CustomVerticalLine. This displayed the two labels but then I couldn’t bind to the X1 value or the text property.
What am I doing wrongly, could anyone help please?
Thanks
- Russell Link asked 5 years ago
- You must login to post comments
Hi there,
Here is some code sample:
public class CustomVerticalAnnotation : VerticalLineAnnotationForMvvm
{
public static readonly DependencyProperty AnnotationLabelsCollectionProperty = DependencyProperty.Register(
"AnnotationLabelsCollection", typeof(AnnotationLabelsCollection), typeof(CustomVerticalAnnotation), new PropertyMetadata(null, OmAnnotationLabelsCollectionChanged));
public AnnotationLabelsCollection AnnotationLabelsCollection
{
get { return (AnnotationLabelsCollection) GetValue(AnnotationLabelsCollectionProperty); }
set { SetValue(AnnotationLabelsCollectionProperty, value); }
}
private static void OmAnnotationLabelsCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ann = (CustomVerticalAnnotation)d;
var newCollection = (AnnotationLabelsCollection)e.NewValue;
var oldCollection = (AnnotationLabelsCollection)e.OldValue;
if (oldCollection != null)
{
foreach (var label in oldCollection)
{
ann.AnnotationLabels.Remove((AnnotationLabel)label);
}
}
if (newCollection != null)
{
foreach (var label in newCollection)
{
ann.AnnotationLabels.Add((AnnotationLabel)label);
}
}
}
}
public class AnnotationLabelsCollection : ObservableCollection<AnnotationLabel>
{
}
public class CustomVerticalAnnotationVM : VerticalLineAnnotationViewModel
{
public override Type ViewType { get { return typeof(CustomVerticalAnnotation); } }
}
<Style x:Key="VertAnn" TargetType="local:CustomVerticalAnnotation">
<Setter Property="AnnotationLabelsCollection">
<Setter.Value>
<local:AnnotationLabelsCollection>
<s:AnnotationLabel LabelPlacement="TopRight" Text="Top-aligned" />
<s:AnnotationLabel LabelPlacement="TopLeft" Text="Topadasdasd-aligned" />
</local:AnnotationLabelsCollection>
</Setter.Value>
</Setter>
</Style>
Please try it out.
Best regards, Taras B.
- Taras Bulka answered 5 years ago
-
Hi there, thank you so much for your answer however I am still having difficulties. I have replicated your classes. How do I add this CustomVerticalAnnotation to my IAnnotationViewModel Collection. All I get is a line without any labels? Thanks Russell
-
I have it working, I didn’t set the correct DataContext for the style. Thank you!
- You must login to post comments
Please login first to submit.