这是我的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
//myOnlyGrid = new Grid();
Chart MyChart = new Chart();
ColumnSeries b = new ColumnSeries();
b.Title = "B";
b.IndependentValueBinding = new System.Windows.Data.Binding("Key");
b.DependentValueBinding = new System.Windows.Data.Binding("Value");
b.ItemsSource = new List<KeyValuePair<string, int>> {
new KeyValuePair<string, int>("1", 100),
new KeyValuePair<string, int>("2", 75),
new KeyValuePair<string, int>("3", 150)
};
MyChart.Series.Add(b);
Grid.SetColumn(MyChart, 0);
Thread.Sleep(1000);
myOnlyGrid.Children.Add(MyChart);
InitializeComponent();
}
和我的 XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="myOnlyGrid">
</Grid>
出于某种原因,它编译得很好,但在到达 myOnlyGrid.Children.Add() 时会抛出 nullreferenceexception。我已经在谷歌上搜索了大约一个小时,但没有找到任何东西。