使用 DataTemplateSelector 动态更改 WPF 工具包图表类型

发布于 2024-12-13 13:42:14 字数 1082 浏览 1 评论 0原文

我有两个 WPF 工具包图表(柱形图和饼图)。

<-- 柱形图 -->

<DVC:Chart Name="mcChartPie" Title="{Binding ChartName}"       
DataContext="{Binding SelectedChart}">
<DVC:Chart.Series>
<DVC:PieSeries ItemsSource="{Binding Columns}" Title="Some Chart"
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}"></DVC:PieSeries>
</DVC:Chart.Series>
</DVC:Chart>

<-- 饼图 -->

    <DVC:Chart Name="mcChart" Title="{Binding ChartName}"    
DataContext="{Binding SelectedChart}" Style="{DynamicResource Info>
<DVC:Chart.Series>
<DVC:ColumnSeries ItemsSource="{Binding Columns}" Title="Some Chart"  
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}" Background="Black" AnimationSequence="FirstToLast" ></DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>

有一个组合框允许用户选择图表类型。当用户选择“柱形图类型”时,柱形图模板应可见,当用户选择“饼图类型”时,饼图模板应可见。如何通过重写 DataTemplateSelector 类中的 SelectTemplate() 方法来做到这一点?

I have two WPF toolkit charts (column and pie).

<-- Column Chart -->

<DVC:Chart Name="mcChartPie" Title="{Binding ChartName}"       
DataContext="{Binding SelectedChart}">
<DVC:Chart.Series>
<DVC:PieSeries ItemsSource="{Binding Columns}" Title="Some Chart"
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}"></DVC:PieSeries>
</DVC:Chart.Series>
</DVC:Chart>

<-- Pie Chart -->

    <DVC:Chart Name="mcChart" Title="{Binding ChartName}"    
DataContext="{Binding SelectedChart}" Style="{DynamicResource Info>
<DVC:Chart.Series>
<DVC:ColumnSeries ItemsSource="{Binding Columns}" Title="Some Chart"  
IndependentValueBinding="{Binding Path=Name}" DependentValueBinding="{Binding 
Path=Value}" Background="Black" AnimationSequence="FirstToLast" ></DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>

There is a combobox that allows user to select the chart type. When user selects the "Column Chart Type", the column chart template should be visible and when user selects "Pie Chart Type", the pie chart template should be visible. How can I do this by overriding SelectTemplate() method in the DataTemplateSelector class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-12-20 13:42:14

尝试这样的方法,只是一个简单的方法,创建一个新的系列(饼图、条形图、柱形图、线条图、面积图),然后在清除图表控件上加载的上一个系列后将该系列添加到图表控件中。

问候

    void loadPieSerie()
    {
        System.Windows.Controls.DataVisualization.Charting.PieSeries pieSerie = new System.Windows.Controls.DataVisualization.Charting.PieSeries();
        chartControl.Series.Clear();

        List<KeyValuePair<string, int>> valueList= new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("Dogs", 50));
        valueList.Add(new KeyValuePair<string, int>("Cats", 20));
        valueList.Add(new KeyValuePair<string, int>("Dinosaurs", 30));
        pieSerie.DependentValuePath = "Value";
        pieSerie.IndependentValuePath = "Key";
        pieSerie.ItemsSource = values;
        chartControl.Series.Add(pieSerie);
    }

Try something like this, just a simple method which creates a new serie (Pie, Bar, Column, Lines , Area), and then adds the serie to the chart control after clearing the previous serie loaded on the chart control.

Regards

    void loadPieSerie()
    {
        System.Windows.Controls.DataVisualization.Charting.PieSeries pieSerie = new System.Windows.Controls.DataVisualization.Charting.PieSeries();
        chartControl.Series.Clear();

        List<KeyValuePair<string, int>> valueList= new List<KeyValuePair<string, int>>();
        valueList.Add(new KeyValuePair<string, int>("Dogs", 50));
        valueList.Add(new KeyValuePair<string, int>("Cats", 20));
        valueList.Add(new KeyValuePair<string, int>("Dinosaurs", 30));
        pieSerie.DependentValuePath = "Value";
        pieSerie.IndependentValuePath = "Key";
        pieSerie.ItemsSource = values;
        chartControl.Series.Add(pieSerie);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文