c#-WPF和XAML中的OxyPlot线注释可见性
作者:互联网
我已经在XAML中使用Plot(而不是PlotView)配置了Oxyplot. (我在单个属性上使用Binding,而不是在完整的PlotModel上使用.)
该配置使用6个LineAnnotations,我想可选地显示或隐藏它们.
我已经使用Binding将LineAnnotations的Visibility属性连接到ViewModel中的属性.这些属性是布尔值,并且我使用了转换器将值转换为Visibility.Collapsed或Visibility.Visible(但我也可以使用Visibility类型定义一个属性).
这行不通. LineAnnotations始终可见.
实际上,我认为LineAnnotation的Visibility属性根本不起作用.
有人可以确认这个问题吗?
有没有其他方法可以基于ViewModel中的属性显示或隐藏LineAnnotations?
我宁愿坚持使用XAML配置,而不是在代码中添加和删除LineAnnotations.
更新资料
根据要求,一些代码
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>
类型转换器
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 ();
}
}
我还有一个附加的行为来处理LineAnnotation上的鼠标事件(这很酷),但是我认为这并不重要.
我也尝试过直接在XAML中指定Visibility =“ Collapsed”(以及“ Hidden”),但是它没有任何效果.
即使绑定不起作用,在XAML中硬编码值也应该起作用.
解决方法:
在OxyPlot和OxyPlot.Wpf的当前实现中,这将不起作用.
类OxyPlot.Wpf.LineAnnotation是内部类OxyPlot.LineAnnotation的包装.要处理Visiblilty属性,必须将其映射到内部LineAnnotation类的适当属性.
有两个问题. Wpf.LineAnnotation不仅不处理Visiblilty属性,而且LineAnnotation类中没有可将其映射到的基础属性. LineAnnotation类似乎没有任何隐藏自身的概念.
这是OxyPlot和XAML之间的不匹配.正如jstreet所指出的,LineAnnotation根本不会出现在可视树中.
标签:oxyplot,wpf,xaml,c,mvvm 来源: https://codeday.me/bug/20191118/2024557.html