gpt4 book ai didi

c# - 使用 WPF 工具包绑定(bind)饼图不显示点

转载 作者:行者123 更新时间:2023-11-30 21:55:53 24 4
gpt4 key购买 nike

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

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

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

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

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

PiePoint 是我这样定义的对象:

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

当我在我的构造函数中启动我的 ViewModel 时,我在这里添加了一些测试数据点以查看我是否可以在我的图表上获取一些数据:

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

但是图表上什么也没有显示。断点命中,我看到图表的背景,我知道项目源绑定(bind)到正确的集合,但我似乎无法让它工作。有什么建议吗?

最佳答案

这是我使用您的数据得到的结果:

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>

结果:

enter image description here

请注意,我正在 XAML 中创建 PieCollection,因此它与图表的 DataContext 相关联。在你的情况下,你正在代码后面创建你的 PieCollection,所以你可能需要做一些类似的事情

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;
}
}

结果:

enter image description here

关于c# - 使用 WPF 工具包绑定(bind)饼图不显示点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com