【问题标题】:Dynamically change WPF Toolkit Chart Types using DataTemplateSelector使用 DataTemplateSelector 动态更改 WPF 工具包图表类型
【发布时间】:2011-12-20 23:32:30
【问题描述】:

我有两个 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() 方法来做到这一点?

【问题讨论】:

  • 唯一的一种方法是使用 2 个数据模板创建 2 个用户控件并切换它们。如何使用DataTemplateSelector 类我在我对这个问题的回答中描述过:stackoverflow.com/questions/5309099/…
  • 谢谢!这真的很有帮助!
  • 你能写出符合系列类型的代码吗?

标签: wpf datatemplate wpftoolkit datatemplateselector


【解决方案1】:

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

问候

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

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 2014-08-10
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多