WPF 代码中的数据绑定问题
我有以下集合,它用作图表的一组 LineSeries ItemsSource 的全局存储。
public ObservableCollection<ObservableCollection<Data>> AllDataSeries;
所述集合可能会时不时地发生变化,当它重置时,我会清除所有系列中的图表 - 当它再次重新填充时,我会向图表中添加与 AllDataSeries.Count
一样多的系列告诉我。
此时我需要设置绑定。
for(int i = 0; i < AllDataSeries.Count; i++)
{
var series = new LineSeries { IndependentValuePath = "X", DependentValuePath = "Y", Title = "SomeSeriesTitle" };
Binding binding = new Binding("#?????#");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
series.SetBinding(ListView.ItemsSourceProperty, binding);
MyChart.Series.Add(series);
}
问题是,我不知道如何编写绑定的路径。我尝试过:
String.Format("AllDataSeries[{0}]", i)
但没有成功。
我应该如何设置绑定的 Path 属性?
I've got the following collection, which serves as a global storage for a group of LineSeries ItemsSources for a chart.
public ObservableCollection<ObservableCollection<Data>> AllDataSeries;
The said collection may change every now and then, when it's reset I clear the chart from all series - and when it's re-populating again I'm adding as many series to the chart as the AllDataSeries.Count
tells me to.
At this point I need to set the binding.
for(int i = 0; i < AllDataSeries.Count; i++)
{
var series = new LineSeries { IndependentValuePath = "X", DependentValuePath = "Y", Title = "SomeSeriesTitle" };
Binding binding = new Binding("#?????#");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
series.SetBinding(ListView.ItemsSourceProperty, binding);
MyChart.Series.Add(series);
}
The thing is, I have no ide how to compose the Path for the binding. I tried :
String.Format("AllDataSeries[{0}]", i)
but it didn't work.
How should I set the Path property on the binding ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AllDataSeries 实际上是一个字段而不是一个属性吗?我不认为 PropertyPath 会遵循公共领域。无论如何,不建议公开公共字段,因此您可能应该将其更改为:
但是要回答有关绑定语法的问题,我相信您的 PropertyPath 语法是正确的。尽管我对这样的嵌套 ObservableCollection 感到紧张。就我个人而言,我会完全放弃绑定并执行类似的操作:
看起来对 AllDataSeries 集合的更改不会触发添加新的系列。
您还记得设置图表的 DataContext 吗?或者,您可以设置绑定的 Source 属性。
最后,您应该将 BindingMode 更改为 OneWay 并删除 UpdateSourceTrigger。图表是只读控件,因此没有理由在那里进行双向绑定。
希望这有帮助。
Is AllDataSeries actually a field and not a property? I don't think PropertyPath will follow a public field. It's not recommended to expose public fields anyway so you should probably just change that to:
But to answer your question about the binding syntax, I believe your PropertyPath syntax is correct. Though I am nervous about a nested ObservableCollection like that. Personally I would just forego the binding altogether and do something like:
It doesn't look like changes to the AllDataSeries collection are going to trigger new series being added anyway.
Did you remember to set the DataContext of the chart? Optionally, you can set the Source property of the Binding.
Finally, you should change BindingMode to OneWay and remove the UpdateSourceTrigger. A chart is a read-only control and so there's no reason to have a two-way binding there.
Hope this helps.