简体   繁体   English

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

[英]Binding Pie Chart with WPF Toolkit not showing points

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>

And my xaml is also referenced here: 我的xaml在这里也被引用:

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

So no problem there. 所以那没问题。 I have a dataset I declare in my ViewModel: 我在ViewModel中声明了一个数据集:

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

Where PiePoint is an object I define like so: PiePoint是我定义的对象,如下所示:

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

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: 当我在构造函数中启动ViewModel时,在此处添加了一些测试数据点,以查看是否可以在图表上获取一些数据:

    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: 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>

Result: 结果:

在此处输入图片说明

Notice that I'm creating the PieCollection within XAML, so it is associated with the DataContext for the chart. 请注意,我正在XAML中创建PieCollection ,因此它与图表的DataContext相关联。 In your case you're creating your PieCollection in code behind, so you probably need to do something like 在您的情况下,您将在后面的代码中创建PieCollection ,因此您可能需要执行以下操作

chart.DataContext = PieCollection

EDIT : creating PieCollection dynamically: 编辑 :动态创建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;
    }
}

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM