LiveCharts(LVC)WPF-从DataClick事件获取图表

Plu*_*orm 3 c# wpf livecharts

我有一个带有多个图表的“仪表盘”。其中之一是带有多个系列的饼图。

LiveCharts有一个DataClick事件

DataClick(object sender, ChartPoint chartPoint)
Run Code Online (Sandbox Code Playgroud)

sender是类型PieSlice。如何SeriesCollection从该事件或图表名称/ ID访问?

我要实现的目标是发送事件的访问图,然后是系列集合并检查哪个系列/饼图触发了该事件。

hya*_*kov 5

首先,不要使用events,而是使用commands-这就是MVVM的方式。即

<LiveCharts:PieChart DataClickCommand="{Binding DrillDownCommand}" Series="{Binding MySeries}" ...>
Run Code Online (Sandbox Code Playgroud)

注意绑定到MySeries

public SeriesCollection MySeries
{
    get
    {
        var seriesCollection = new SeriesCollection(mapper);

        seriesCollection.Add(new PieSeries()
        {
            Title = "My display name",
            Values = new ChartValues<YourObjectHere>(new[] { anInstanceOfYourObjectHere })
        });

        return seriesCollection;
    }
}
Run Code Online (Sandbox Code Playgroud)

关于命令的处理:

public ICommand DrillDownCommand
{
    get
    {
        return new RelayCommand<ChartPoint>(this.OnDrillDownCommand);
    }
}

private void OnDrillDownCommand(ChartPoint chartPoint)
{
    // access the chartPoint.Instance (Cast it to YourObjectHere and access its properties)
}
Run Code Online (Sandbox Code Playgroud)