简体   繁体   English

WPF和XAML中的OxyPlot LineAnnotation可见性

[英]OxyPlot LineAnnotation Visibility in WPF and XAML

I have configured an Oxyplot in XAML, using Plot, not PlotView. 我在XAML中使用Plot而不是PlotView配置了一个Oxyplot。 (I am using Binding on individual properties, rather than on the complete PlotModel.) (我在个别属性上使用Binding,而不是在完整的PlotModel上使用。)

The configuration uses 6 LineAnnotations, which I want to show or hide optionally. 配置使用6个LineAnnotations,我想要显示或隐藏它。

I have used Binding to connect the Visibility property of the LineAnnotations to properties in my ViewModel. 我使用Binding将LineAnnotations的Visibility属性连接到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). 属性是布尔值,我使用转换器将值转换为Visibility.Collapsed或Visibility.Visible(但我也可以定义一个类型为Visibility的属性)。

This doesn't work. 这不起作用。 The LineAnnotations are always visible. LineAnnotations始终可见。

In fact, I don't think that the Visibility property of a LineAnnotation works at all. 实际上,我认为LineAnnotation的Visibility属性根本不起作用。

Can anybody confirm this problem? 谁能证实这个问题?

Is there an alternative way to show or hide LineAnnotations based on a property in my ViewModel? 是否有另一种方法可以根据我的ViewModel中的属性显示或隐藏LineAnnotations?

I would prefer to stick with the XAML configuration, rather than add and remove the LineAnnotations in code. 我更喜欢坚持使用XAML配置,而不是在代码中添加和删除LineAnnotations。

Update 更新

As requested, some code 根据要求,一些代码

Xaml 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. 我还有一个附加的行为来处理LineAnnotation上的鼠标事件(这很酷),但我不认为它是相关的。

I have also tried specifying Visibility="Collapsed" (and also "Hidden") directly in XAML, but it has no effect. 我也尝试在XAML中直接指定Visibility =“Collapsed”(以及“隐藏”),但它没有任何效果。

Even if the binding was not working, hard coding the value in XAML ought to work. 即使绑定不起作用,在XAML中对值进行硬编码也应该有效。

In the current implementation of OxyPlot and OxyPlot.Wpf this will not work. 在OxyPlot和OxyPlot.Wpf的当前实现中,这将不起作用。

The class OxyPlot.Wpf.LineAnnotation is a wrapper for the internal class OxyPlot.LineAnnotation. OxyPlot.Wpf.LineAnnotation类是内部类OxyPlot.LineAnnotation的包装器。 To handle the Visiblilty property, it would have to map the property to an appropriate property of the internal LineAnnotation class. 要处理Visiblilty属性,必须将属性映射到内部LineAnnotation类的相应属性。

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. Wpf.LineAnnotation不仅不处理Visiblilty属性,而且LineAnnotation类中没有底层属性可以映射到它。 The LineAnnotation class doesn't appear to have any concept of making itself hidden. LineAnnotation类似乎没有隐藏任何隐藏的概念。

This is a mismatch between OxyPlot and XAML. 这是OxyPlot和XAML之间的不匹配。 As jstreet has pointed out, LineAnnotation does not appear in the Visual Tree at all. 正如jstreet指出的那样,LineAnnotation根本没有出现在Visual Tree中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM