Checkbox for binding LegendItem to row visibility in WPF

My WPFToolKit table contains several series. I myself masked the legend and also launched the LegendItem template, creating a style resource:

<Style x:Key="CustomLegendItemStyle" TargetType="{x:Type charting:LegendItem}"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type charting:LegendItem}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <DockPanel LastChildFill="True"> <Ellipse Width="10" Height="10" Fill="{Binding Background}" Stroke="{Binding Background}" StrokeThickness="1" Margin="0,0,3,0" DockPanel.Dock="Left"/> <CheckBox IsChecked="{Binding Path=Visibility,Converter={StaticResource VisToBoolConverter},Mode=TwoWay}" /> <TextBlock DockPanel.Dock="Right" Text="(num)" /> <datavis:Title Content="{TemplateBinding Content}" Margin="10 0" /> </DockPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type charting:LineSeries}"> <Setter Property="LegendItemStyle" Value="{StaticResource CustomLegendItemStyle}" /> </Style> 

This creates a checkbox in LegendItem that should control the visibility of the series. But this is not so. I also created properties in the ViewModel (which by default is true / visible) and with which the LineSeries binding is bound to

 <charting:LineSeries ... Visibility="{Binding DisplayLoad,Converter={StaticResource BoolToVisConverter},Mode=TwoWay}" /> 

But these two are not connected. If I changed the flag binding path to StoopidUser, I get a binding error in the output window informing me that the StoopidUser property was not found on the LineDataPoint object, which puzzles me a bit. I checked directly and do not see (a) why it is LineDataPoint (b) how to get to the series from it.

Do you see what is wrong?

+4
source share
2 answers

Finally, I was able to do this. Here is my solution. enter image description here

  <PointCollection x:Key="Serie1Key">1,10 2,20 3,30 4,40</PointCollection> <PointCollection x:Key="Serie2Key">2,10 4,20 6,30 8,40</PointCollection> <Style x:Key="CustomLegendItemStyle" TargetType="{x:Type chartingToolkit:LegendItem}"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="chartingToolkit:LegendItem"> <StackPanel Orientation="Horizontal"> <CheckBox VerticalAlignment="Center" IsChecked="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Owner.Visibility, Mode=TwoWay, Converter={StaticResource VisibilityToBoolConverter}}" /> <Rectangle VerticalAlignment="Center" Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="5,0,5,0" /> <visualizationToolkit:Title VerticalAlignment="Center" Content="{TemplateBinding Content}" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid x:Name="LayoutRoot"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <chartingToolkit:Chart x:Name="chart" HorizontalAlignment="Left" Title="Chart Title" VerticalAlignment="Top" > <chartingToolkit:LineSeries DependentValuePath="X" IndependentValuePath="Y" ItemsSource="{StaticResource Serie1Key}" LegendItemStyle="{StaticResource CustomLegendItemStyle}"/> <chartingToolkit:LineSeries DependentValuePath="X" IndependentValuePath="Y" ItemsSource="{StaticResource Serie2Key}" LegendItemStyle="{StaticResource CustomLegendItemStyle}"/> </chartingToolkit:Chart> </Grid> 
+5
source

I didn’t understand. But deleting the legend and giving my own opportunity provided me with the opportunity to communicate, as I expected it to work. I would like to know where I am mistaken, although if someone finds out ...

0
source

Source: https://habr.com/ru/post/1434916/


All Articles