无法将整个图表绑定到前端

本文关键字:绑定 前端 | 更新日期: 2023-09-27 18:18:36

我正在寻找一种在我的xaml中绑定整个图表的方法。不仅仅是一个系列,而是整个图表。这是由于我需要在图表中动态添加多个系列。

XAML:

<chart:Chart DataContext="{Binding Path=FocusEnergyChart, UpdateSourceTrigger=PropertyChanged}"/> 
c#

:

public void DrawChart()
    {
        myChart = new Chart();
        LinearAxis xAxis = new LinearAxis();
        xAxis.Orientation = AxisOrientation.X;
        xAxis.Title = "Energy";
        LinearAxis yAxis = new LinearAxis();
        yAxis.Orientation = AxisOrientation.Y;
        yAxis.Title = "Focus";
        myChart.Axes.Add(xAxis);
        myChart.Axes.Add(yAxis);
        myChart.Title = "Focus Energy - Chart";
        foreach( string aKey in myGraphData.Keys)
        {
            BubbleSeries aSeries = new BubbleSeries();
            aSeries.Title = aKey;
            aSeries.ItemsSource = myGraphData[aKey];
            aSeries.DependentValuePath = "Focus";
            aSeries.IndependentValuePath = "Energy";
            aSeries.SizeValuePath = "Size";
            aSeries.SetResourceReference(Series.BackgroundProperty, "Background");
            aSeries.SetResourceReference(BubbleSeries.LegendItemStyleProperty, "CustomLegendItemStyle");
            aSeries.SetResourceReference(BubbleSeries.DataPointStyleProperty, "BubbleToolTipTemplate");
            myChart.Series.Add(aSeries);
        }
        base.OnPropertyChanged("FocusEnergyChart");
    }
    public Chart FocusEnergyChart
    {
        get { return myChart; }
    }

当我尝试运行这段代码时,它只是向我显示一个空的ChartArea。

我希望有人能帮助我!

谢谢!

无法将整个图表绑定到前端

您在这里所做的就是添加另一个图表作为从XAML声明的图表的DataContext

您可以使用ContentControl并将其内容绑定到从代码后面创建的图表:

<ContentControl Content="{Binding Path=FocusEnergyChart}" />