将图表LegendItem复选框绑定到WPF中的系列可见性

时间:2012-09-18 18:38:34

标签: wpf xaml mvvm charts wpftoolkit

我的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 / visible),以及LineSeries可见性绑定到的属性

<charting:LineSeries ... Visibility="{Binding DisplayLoad,Converter={StaticResource BoolToVisConverter},Mode=TwoWay}" />
但是这两个人并没有联系起来。如果我将复选框绑定路径更改为StoopidUser,我会在输出窗口中收到绑定错误,告诉我在对象LineDataPoint上找不到StoopidUser属性,这让我有点难过。我已经检查过,无法看到(a)为什么它是LineDataPoint(b)如何从中获取该系列。

你能看出什么是错的吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我从未弄清楚这一点。但删除传说并把我自己放在那里为我提供了绑定的机会,因为我期望它能够工作。如果有人发现,我仍然想知道我错在哪里......