C# 使用WPF工具包绑定饼图时不显示点
我有一个piechart,我定义如下:C# 使用WPF工具包绑定饼图时不显示点,c#,wpf,xaml,mvvm,charts,C#,Wpf,Xaml,Mvvm,Charts,我有一个piechart,我定义如下: <chartingToolkit:Chart DataContext="1,10 2,20 3,30 4,40" HorizontalAlignment="Left" Margin="334,238,0,0" Name="chart1" VerticalAlignment="Top" Height="177"
<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>
所以没问题。我在ViewModel中声明了一个数据集:
private ObservableCollection<PiePoint> _pieCollection;
public ObservableCollection<PiePoint> PieCollection { get { return _pieCollection; } set { _pieCollection = value; OnPropertyChanged("PieCollection"); } }
当我在构造函数中启动ViewModel时,我在此处添加了一些测试数据点,以查看是否可以在图表中获得一些数据:
PieCollection = new ObservableCollection<PiePoint>();
PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });
PieCollection=newObservableCollection();
添加(新的PiePoint{Name=“Mango”,Share=10});
添加(新的PiePoint{Name=“Banana”,Share=36});
但图表上什么也没有显示。断点命中,我看到了图表的背景,我知道items源绑定到了正确的集合,但我似乎无法让它工作。有什么建议吗?以下是我使用您的数据得到的: XAML: 编辑:动态创建
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;
}
}
结果:
我之所以在后台代码中创建我的PieCollection,是因为这些值是动态变化的。此外,由于它位于viewmodel中,我将无法访问我的饼图对象以保持界面分离。正如我所说,您没有将
PieCollection
分配给DataContext
。我相应地更新了我的答案。另外,我认为您在XAML中不需要这个:DataContext=“1,10 2,20 3,30 4,40”
。把它拿出来。
PieCollection = new ObservableCollection<PiePoint>();
PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });
<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>
chart.DataContext = 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;
}
}