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/visible),并且 LineSeries Visibility 绑定到

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

但两人并没有勾搭上。如果我将复选框绑定路径更改为 StoopidUser,我会在输出窗口中收到一个绑定错误,告诉我在 object 上找不到 StoopidUser 属性LineDataPoint,这让我有点难过。我已经检查过了,看不到 (a) 为什么它是 LineDataPoint (b) 如何从中获取系列。

你能看出有什么问题吗?

4

2 回答 2

5

最后我能够做到。这是我的解决方案。 在此处输入图像描述

    <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>
于 2012-10-30T16:11:31.807 回答
0

我从来没有完全弄清楚这一点。但是删除图例并将我自己的图例放在那里为我提供了按预期绑定的机会。仍然想知道我错在哪里,但如果有人发现......

于 2012-09-27T17:19:24.923 回答