使用WPF工具包绑定饼图不显示点

问题描述:

我有一个定义如下的饼图:

I have a piechart that I have defined as below:

        <chartingToolkit:Chart DataContext="1,10 2,20 3,30 4,40" 
                           HorizontalAlignment="Left" Margin="334,238,0,0" 
                           Name="chart1"  VerticalAlignment="Top" Height="177"
                           Width="218" BorderBrush="#00000000">
        <chartingToolkit:PieSeries 
        ItemsSource="{Binding PieCollection}"
        IndependentValueBinding="{Binding Path=Name}"
        DependentValueBinding="{Binding Path=Share}" />
    </chartingToolkit:Chart>

这里也引用了我的xaml:

And my xaml is also referenced here:

 xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">

所以那里没有问题。我在ViewModel中声明了一个数据集:

So no problem there. I have a dataset I declare in my ViewModel:

 private ObservableCollection<PiePoint> _pieCollection;
 public ObservableCollection<PiePoint> PieCollection { get { return _pieCollection; } set { _pieCollection = value; OnPropertyChanged("PieCollection"); } }

其中PiePoint是我定义的对象,如下所示:

Where PiePoint is an object I define like so:

    public class PiePoint
    {
        public string Name { get; set; }
        public Int16 Share { get; set; }
    }

在构造函数中启动ViewModel时,我添加了一些测试数据指向此处以查看是否可以从图表上获得一些数据:

When I start up my ViewModel in my constructor, I added some test data points here to see if I could get some data on my chart:

    PieCollection = new ObservableCollection<PiePoint>();
    PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
    PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });

但图表上没有任何显示。达到断点的时候,我看到了图表的背景,并且我知道项目源绑定到了正确的集合,但是我似乎无法使其正常工作。有任何建议吗?

But nothing shows up on the chart. The breakpoint hits, I see the chart's background, and I know that the items source is bound to the right collection, but I can't seem to get it to work. Any suggestions?

这是我使用您的数据所得到的信息:

Here's what I got using your data:

XAML:

<Grid>
    <chartingToolkit:Chart Margin="0" Title="Chart Title">
        <chartingToolkit:Chart.DataContext>
            <local:PieCollection/>
        </chartingToolkit:Chart.DataContext>
        <chartingToolkit:PieSeries ItemsSource="{Binding Mode=OneWay}" DependentValuePath="Share" IndependentValuePath="Name" DataContext="{Binding Mode=OneWay}" >
        </chartingToolkit:PieSeries>
    </chartingToolkit:Chart>
</Grid>

结果:

请注意,我正在XAML中创建 PieCollection ,因此与图表的 DataContext 关联。在您的情况下,您将在后面的代码中创建 PieCollection ,因此您可能需要执行类似的操作

Notice that I'm creating the PieCollection within XAML, so it is associated with the DataContext for the chart. In your case you're creating your PieCollection in code behind, so you probably need to do something like

chart.DataContext = PieCollection

编辑 :动态创建 PieCollection

public partial class MainWindow : Window
{
    PieCollection pieCollection1;

    public MainWindow()
    {
        InitializeComponent();

        pieCollection1 = new PieCollection();

        pieCollection1.Add(new PiePoint { Name = "Mango", Share = 10 });
        pieCollection1.Add(new PiePoint { Name = "Banana", Share = 36 });
        pieCollection1.Add(new PiePoint { Name = "Grapes", Share = 15 });
        pieCollection1.Add(new PiePoint { Name = "Apple", Share = 20 });

        Chart1.DataContext = pieCollection1;
    }
}

结果: