绑定 List<KeyValuePair> 时折线图只有一个轴

Line Chart Has only One Axis when bind List<KeyValuePair>

好吧,我有下一个问题 - 在页面上我有 WPF 工具包图表、折线图。

<chartingToolkit:Chart Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" Name="lineChart" Visibility="Hidden" VerticalAlignment="Top" Height="150" BorderThickness="3" BorderBrush="#FF423852">
                        <chartingToolkit:Chart.Axes>
                            <chartingToolkit:LinearAxis Orientation="Y" Minimum="0">
                                <chartingToolkit:LinearAxis.MajorTickMarkStyle>
                                    <Style TargetType="Line">
                                        <Setter Property="Stroke" Value="#bdb3ce" />
                                        <Setter Property="StrokeThickness"  Value="1" />
                                        <Setter Property="Y1"   Value="-4" />
                                        <Setter Property="Y2"  Value="4" />
                                    </Style>
                                </chartingToolkit:LinearAxis.MajorTickMarkStyle>
                            </chartingToolkit:LinearAxis>
                        </chartingToolkit:Chart.Axes>

                        <chartingToolkit:Chart.Style>
                            <Style TargetType="Control">
                                <Setter Property="Foreground" Value="#bdb3ce" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="chartingToolkit:Chart">
                                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Name="BorderParent">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition Height="*" />
                                                    </Grid.RowDefinitions>

                                                    <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                                                    <Grid Grid.Row="1" Margin="5 15">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="*" />
                                                            <ColumnDefinition Width="Auto" />
                                                        </Grid.ColumnDefinitions>

                                                        <primitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                                            <Grid x:Name="PlotArea" Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}"/>
                                                            <Border Canvas.ZIndex="10" BorderBrush="#61596f" BorderThickness="1 0 0 1" Margin="5 2"/>
                                                            <Canvas  Background="Transparent" x:Name="activePointGridLines" Margin="5 0 0 0" />
                                                        </primitives:EdgePanel>
                                                    </Grid>
                                                </Grid>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </chartingToolkit:Chart.Style>
                        <chartingToolkit:Chart.LegendStyle>
                            <Style TargetType="Control">
                                <Setter Property="Width" Value="0" />
                            </Style>
                        </chartingToolkit:Chart.LegendStyle>
                        <chartingToolkit:Chart.PlotAreaStyle>
                            <Style TargetType="Grid">
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="SnapsToDevicePixels" Value="True" />
                            </Style>
                        </chartingToolkit:Chart.PlotAreaStyle>
                        <chartingToolkit:LineSeries IndependentValuePath="Key" DependentValuePath="Value" ItemsSource="{Binding}" DataContext="{Binding}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" Name="nLineSeries">
                            <chartingToolkit:LineSeries.IndependentAxis>
                                <chartingToolkit:CategoryAxis Orientation="X" MajorTickMarkStyle="{StaticResource ChartMajorTickMarkStyle}"/>
                            </chartingToolkit:LineSeries.IndependentAxis>
                        </chartingToolkit:LineSeries>
                    </chartingToolkit:Chart>

当我转到此页面时,lineChart 可以为空,即使数据已绑定到图表 DataContext。我注意到,当图表为空时,它只有一个轴(Y 轴)。当图表不为空时,绑定图表后有2个轴。

private void InitLineChart()
    {
        var valueList = new List<KeyValuePair<string, int>>();
        //fill List with data
        _lineChart.DataContext = valueList;
    }

什么会导致此行为以及如何解决?

问题已解决。

我删除了IndependentAxis

发件人 :

<chartingToolkit:LineSeries IndependentValuePath="Key" DependentValuePath="Value" ItemsSource="{Binding}" DataContext="{Binding}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" Name="nLineSeries">
       <chartingToolkit:LineSeries.IndependentAxis>
              <chartingToolkit:CategoryAxis Orientation="X" MajorTickMarkStyle="{StaticResource ChartMajorTickMarkStyle}"/>
       </chartingToolkit:LineSeries.IndependentAxis>
</chartingToolkit:LineSeries>

:

<chartingToolkit:LineSeries IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" Margin="5.5,0,0,3" DataPointStyle="{StaticResource SeriesDataPointStyle}" />

现在绑定数据不是绑定到图表的 DataContext,而是绑定到存在的 LineSeriesItemsSource

发件人 :

_lineChart.DataContext = valueList;

var lineChartSerie = (LineSeries)_lineChart.Series.FirstOrDefault();
if (lineChartSerie != null) lineChartSerie.ItemsSource = valueList;