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

LiveCharts (LVC) WPF - get chart from DataClick event

我有一个 'dashboard',上面有几个图表。其中之一是带有多个系列的饼图。

LiveCharts 有一个 DataClick 事件

DataClick(object sender, ChartPoint chartPoint)

senderPieSlice 类型。我如何从该事件或图表名称/ID 访问 SeriesCollection

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

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

<LiveCharts:PieChart DataClickCommand="{Binding DrillDownCommand}" Series="{Binding MySeries}" ...>

注意绑定到 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;
    }
}

关于处理命令:

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)
}

您需要处理参数,而不是发件人。第二个参数是ChartPoint,包含SeriesView。所以只需访问它并使用它 Title:

    private void Chart_OnDataClick(object sender, ChartPoint chartpoint) {
        MessageBox.Show(chartpoint.SeriesView.Title);
    }

How can i access SeriesCollection from that event or, alternatively, chart name / id?

SeriesView 不是整个 SeriesCollection,而是您单击的 Series。你可以得到它的名字