饼图不显示 WPF

Pie chart not displayed WPF

我使用 livechart 来创建饼图,但是每当我 运行 程序时,都看不到图表 (http://imgur.com/GWecwgD), but I can see the chart when I'm editing the code. (http://imgur.com/zVvkK3v)

这是我的 WPF 代码:

        <lvc:PieChart Series="{Binding seriesCollection}" Height="150" InnerRadius="100" LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="True">
            <lvc:PieChart.ChartLegend>
                <lvc:DefaultLegend BulletSize="20"></lvc:DefaultLegend>
            </lvc:PieChart.ChartLegend>
            <lvc:PieChart.DataTooltip>
                <lvc:DefaultTooltip BulletSize="20"></lvc:DefaultTooltip>
            </lvc:PieChart.DataTooltip>
        </lvc:PieChart>

这是我的 C# 代码:

public Overview()
{
    InitializeComponent();
    NorthwindEntities db = new NorthwindEntities();
    var data = (from d in db.Sales_by_Categories group d by d.CategoryName into grouped select new { Key = grouped.Key, Sum = grouped.Sum(e => (double)e.ProductSales) });
    IEnumerable<Categorysales> datas = from c in data.AsEnumerable() select new Categorysales(c.Key, c.Sum);


    seriesCollection = new SeriesCollection();
    foreach (var item in datas)
    {
        seriesCollection.Add(new PieSeries { Title = item.Categoryname, Values = new ChartValues<ObservableValue> { new ObservableValue(item.Categorysum) }, DataLabels = true});//, LabelPoint = PointLabel 
    }
   /* PointLabel = chartPoint =>
        string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);*/

   // DataContext = this;
}
public SeriesCollection seriesCollection { get; set; }



public Func<ChartPoint, string> PointLabel { get; set; }

private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
{
    var chart = (LiveCharts.Wpf.PieChart)chartpoint.ChartView;

    //clear selected slice.
    foreach (PieSeries series in chart.Series)
        series.PushOut = 0;

    var selectedSeries = (PieSeries)chartpoint.SeriesView;
    selectedSeries.PushOut = 8;
}

这是在 Window 还是用户控件中?

如果是 window,请将绑定更改为:

<lvc:PieChart 
    Series="{Binding seriesCollection, RelativeSource={RelativeSource AncestorType=Window}}"

如果它是一个 UserControl,您知道它的去向:

<lvc:PieChart 
    Series="{Binding seriesCollection, RelativeSource={RelativeSource AncestorType=UserControl}}"

DataContext = this; 是个坏习惯。开始使用 UserControls 这样做,它会破坏事情。即使在 Window 中,它也会造成不必要的混乱。