在WPF中将图表图例项复选框绑定到系列可见性

4

我的WPFToolKit图表包含几个系列。我已经将图例本身进行了模板化,并通过创建样式资源来对LegendItem进行了模板化:

<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>

这在LegendItem中创建了一个复选框,它应该控制系列的可见性。但实际上并没有起到作用。我也在ViewModel上创建了属性(默认为true / 可见),LineSeries的Visibility与之绑定。
<charting:LineSeries ... Visibility="{Binding DisplayLoad,Converter={StaticResource BoolToVisConverter},Mode=TwoWay}" />

但是这两者并没有连接起来。如果我将复选框绑定路径更改为StoopidUser,那么输出窗口中会出现绑定错误,告诉我LineDataPoint对象上找不到StoopidUser属性,这让我有点困惑。我已经仔细检查过了,但无法看出(a)为什么它是一个LineDataPoint(b)如何从中获取系列。

你能看出问题在哪里吗?

2个回答

5
最终我成功了。这是我的解决方案。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>

我在传递给 LineSeriesItemsSource 中的对象中有一个 Visible 属性,以便我可以单独隐藏图表中的每条线。我只需要将其绑定到 LegendItem 上,这样它也会被隐藏(不显示线条但在图例中出现是很奇怪的)。 - Doc

0

我从来没有完全弄清楚这个问题。但是,移除图例并放置自己的图例使我有机会按照我期望的方式进行绑定。不过,如果有人找到了答案,我仍然想知道我错在哪里...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接