WPF 代码中的数据绑定问题

发布于 2024-08-21 14:46:35 字数 882 浏览 8 评论 0原文

我有以下集合,它用作图表的一组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-08-28 14:46:35

AllDataSeries 实际上是一个字段而不是一个属性吗?我不认为 PropertyPath 会遵循公共领域。无论如何,不​​建议公开公共字段,因此您可能应该将其更改为:

public ObservableCollection<ObservableCollection<Data>> AllDataSeries
{
    get;
    private set;
}

但是要回答有关绑定语法的问题,我相信您的 PropertyPath 语法是正确的。尽管我对这样的嵌套 ObservableCollection 感到紧张。就我个人而言,我会完全放弃绑定并执行类似的操作:

series.ItemsSource = AllDataSeries[i];

看起来对 AllDataSeries 集合的更改不会触发添加新的系列。

您还记得设置图表的 DataContext 吗?或者,您可以设置绑定的 Source 属性。

binding.Source = this;

最后,您应该将 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:

public ObservableCollection<ObservableCollection<Data>> AllDataSeries
{
    get;
    private set;
}

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:

series.ItemsSource = AllDataSeries[i];

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.

binding.Source = this;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文