WPF饼图 - 图表上未显示切片

时间:2012-04-11 04:10:02

标签: c# wpf charts pie-chart codeplex

我正在尝试使用WPF饼图。问题是只显示最后一个切片。我可以看到除了最后一片之外,所有切片都被显示并立即隐藏。有什么想法吗?

<Window x:Class="ScrumApp01.WindowGraphique"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:ScrumApp01"
    Title="Fenêtre graphiques SCRUM" Height="674" Width="1216" ResizeMode="CanResizeWithGrip" Name="GraphicScrum01">

<DVC:Chart Margin="25,268,0,0" Title="Pointe de tarte" LegendTitle="Les datas" Background="LightSteelBlue" HorizontalAlignment="Left" VerticalAlignment="Top" Height="324" Width="307">
<DVC:PieSeries Title="Les datas en tarte" Name="Tarte2"
    ItemsSource="{StaticResource FruitCollection2}"
    IndependentValueBinding="{Binding Path=sss}"
    DependentValueBinding="{Binding Path=nnn}" >
</DVC:PieSeries>

private void button3_Click(object sender, RoutedEventArgs e)
{
  FruitCollection2 fff = new FruitCollection2();
  fff.Clear();
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[0], bbb = 1, ddd = 10, sss =   "Item a" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[1], bbb = 2, ddd = 9, sss = "Item b" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[2], bbb = 3, ddd = 11, sss = "Item c" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[3], bbb = 1, ddd = 14, sss = "Item d" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[4], bbb = 4, ddd = 7, sss = "Item e" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[5], bbb = 5, ddd = 1, sss = "Item f" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[6], bbb = 6, ddd = 7, sss = "Item g" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[7], bbb = 3, ddd = 6, sss = "Item h" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[8], bbb = 3, ddd = 7, sss = "Item i" });
  fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[9], bbb = 4, ddd = 5, sss = "Item j" });


  this.Tarte2.ItemsSource = fff;

}

1 个答案:

答案 0 :(得分:0)

在Chart上设置ItemsSource的方式很奇怪。这样做应该有效:

sss定义的类{Binding Path=sss}(基本上是DVC的DataContext:PieSeries)中,声明此属性:

private ObservableCollection<Fruit02> mFff = new ObservableCollection<Fruit02>();
public ObservableCollection<Fruit02> fff
{
    get { return mFff; }
}

然后绑定你的DVC:PieSeries ItemSource:

<DVC:PieSeries Title="Les datas en tarte" Name="Tarte2"
    ItemsSource="{Binding Path=fff}"
    IndependentValueBinding="{Binding Path=sss}"
    DependentValueBinding="{Binding Path=nnn}" >
</DVC:PieSeries>

然后在您的点击处理程序中,不要创建新的集合,只需修改现有的集合:

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        fff.Clear();
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[0], bbb = 1, ddd = 10, sss = "Item a" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[1], bbb = 2, ddd = 9, sss = "Item b" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[2], bbb = 3, ddd = 11, sss = "Item c" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[3], bbb = 1, ddd = 14, sss = "Item d" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[4], bbb = 4, ddd = 7, sss = "Item e" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[5], bbb = 5, ddd = 1, sss = "Item f" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[6], bbb = 6, ddd = 7, sss = "Item g" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[7], bbb = 3, ddd = 6, sss = "Item h" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[8], bbb = 3, ddd = 7, sss = "Item i" });
        fff.Add(new Fruit02 { nnn = this.data001.listeNombres03[9], bbb = 4, ddd = 5, sss = "Item j" });
    }

假设您的DataContext设置正确并且您的饼图控件已正确实现,这应该可以正常工作。