OxyPlot LineAnnotation Visibility in WPF and XAML

2.6k views Asked by At

I have configured an Oxyplot in XAML, using Plot, not PlotView. (I am using Binding on individual properties, rather than on the complete PlotModel.)

The configuration uses 6 LineAnnotations, which I want to show or hide optionally.

I have used Binding to connect the Visibility property of the LineAnnotations to properties in my ViewModel. The properties are boolean and I have used a converter to convert the value to Visibility.Collapsed or Visibility.Visible (but I could just as well have defined a property with the type Visibility).

This doesn't work. The LineAnnotations are always visible.

In fact, I don't think that the Visibility property of a LineAnnotation works at all.

Can anybody confirm this problem?

Is there an alternative way to show or hide LineAnnotations based on a property in my ViewModel?

I would prefer to stick with the XAML configuration, rather than add and remove the LineAnnotations in code.

Update

As requested, some code

Xaml

<oxy:Plot Grid.Row="1">
  <oxy:Plot.Axes>
    <oxy:LinearAxis Key="yAxis"
                    Position="{Binding Path=XAxisInverted, Converter={StaticResource BoolToAxisPosition_fl_tr}}"
                    Minimum="{Binding YAxis_Min}"
                    Maximum="{Binding YAxis_Max}"
                    MajorGridlineStyle="Dot"
                    MajorGridlineColor="LightGray"
                    Title="Velocity [km/h]" />
    <oxy:LinearAxis Key="xAxis"
                    Position="Bottom"
                    Minimum="{Binding XAxisNumericMin}"
                    Maximum="{Binding XAxisNumericMax}"
                    StartPosition="{Binding Path=XAxisInverted, Converter={StaticResource BoolToInt_f0_t1}}"
                    EndPosition="{Binding Path=XAxisInverted, Converter={StaticResource BoolToInt_f1_t0}}"
                    MajorGridlineStyle="Dot"
                    MajorGridlineColor="LightGray"
                    Title="Position [m]" />
  </oxy:Plot.Axes>
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding PointsRef1}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="DarkGray"
                    MarkerType="None"/>
    <oxy:LineSeries ItemsSource="{Binding PointsRef2}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="DarkGray"
                    MarkerType="None"/>
    <oxy:LineSeries ItemsSource="{Binding PointsBlack}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="Black"
                    MarkerType="None"/>
    <oxy:LineSeries ItemsSource="{Binding PointsRed}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="Red"
                    MarkerType="None"/>
    <oxy:LineSeries ItemsSource="{Binding PointsBlue}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="Blue"
                    MarkerType="None"/>
    <oxy:LineSeries ItemsSource="{Binding PointsGreen}"
                    DataFieldX="X"
                    DataFieldY="Y"
                    StrokeThickness="1"
                    MarkerSize="0"
                    LineStyle="Solid"
                    Color="Green"
                    MarkerType="None"/>
  </oxy:Plot.Series>
  <oxy:Plot.Annotations>
    <oxy:LineAnnotation x:Name="BrakePosRear"   Type="Vertical" Color="{Binding BrakePosRear_1_Color}"  Visibility="{Binding Path=BrakePosRear_1_Enabled,  Converter={StaticResource BoolToVisibility}}" X="{Binding BrakePosRear_1_Value}"  local:AnnotationDragableBehavior.IsDragable="True" />
    <oxy:LineAnnotation x:Name="BrakePosFront"  Type="Vertical" Color="{Binding BrakePosFront_1_Color}" Visibility="{Binding Path=BrakePosFront_1_Enabled, Converter={StaticResource BoolToVisibility}}" X="{Binding BrakePosFront_1_Value}" local:AnnotationDragableBehavior.IsDragable="True" />
    <oxy:LineAnnotation x:Name="RefPos"         Type="Vertical" Color="{Binding RefPos_1_Color}"        Visibility="{Binding Path=RefPos_1_Enabled,        Converter={StaticResource BoolToVisibility}}" X="{Binding RefPos_1_Value}"        local:AnnotationDragableBehavior.IsDragable="True" />
    <oxy:LineAnnotation x:Name="BrakePosRear2"  Type="Vertical" Color="{Binding BrakePosRear_2_Color}"  Visibility="{Binding Path=BrakePosRear_2_Enabled,  Converter={StaticResource BoolToVisibility}}" X="{Binding BrakePosRear_2_Value}"  local:AnnotationDragableBehavior.IsDragable="True" />
    <oxy:LineAnnotation x:Name="BrakePosFront2" Type="Vertical" Color="{Binding BrakePosFront_2_Color}" Visibility="{Binding Path=BrakePosFront_2_Enabled, Converter={StaticResource BoolToVisibility}}" X="{Binding BrakePosFront_2_Value}" local:AnnotationDragableBehavior.IsDragable="True" />
    <oxy:LineAnnotation x:Name="RefPos2"        Type="Vertical" Color="{Binding RefPos_2_Color}"        Visibility="{Binding Path=RefPos_2_Enabled,        Converter={StaticResource BoolToVisibility}}" X="{Binding RefPos_2_Value}"        local:AnnotationDragableBehavior.IsDragable="True" />
  </oxy:Plot.Annotations>
</oxy:Plot>

The type converter

class BoolToVisibility : IValueConverter
{
  public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
  {
    return System.Convert.ToBoolean ( value ) ? Visibility.Visible : Visibility.Collapsed ;
  }

  public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
  {
    throw new NotImplementedException ();
  }
}

I also have an attached behavior to handle mouse events on the LineAnnotation (which is pretty cool), but I don't think it is relevant.

I have also tried specifying Visibility="Collapsed" (and also "Hidden") directly in XAML, but it has no effect.

Even if the binding was not working, hard coding the value in XAML ought to work.

1

There are 1 answers

2
Phil Jollans On

In the current implementation of OxyPlot and OxyPlot.Wpf this will not work.

The class OxyPlot.Wpf.LineAnnotation is a wrapper for the internal class OxyPlot.LineAnnotation. To handle the Visiblilty property, it would have to map the property to an appropriate property of the internal LineAnnotation class.

There are two problems. Not only does Wpf.LineAnnotation not handle the Visiblilty property, but there is no underlying property in the LineAnnotation class to which it could be mapped. The LineAnnotation class doesn't appear to have any concept of making itself hidden.

This is a mismatch between OxyPlot and XAML. As jstreet has pointed out, LineAnnotation does not appear in the Visual Tree at all.