ObservableCollection 作为 DependenceProperty 和绑定问题
我创建了一个图表控件,过了一段时间我决定向它添加多系列功能。我使用 ObservableCollection
类型的依赖属性实现了该功能,如下所示:
private static DependencyPropertyKey SeriesPropertyKey = DependencyProperty.RegisterReadOnly("Series", typeof(ObservableCollection<ChartSerie>), typeof(Chart), new FrameworkPropertyMetadata(new ObservableCollection<ChartSerie>()));
public static DependencyProperty SeriesProperty = SeriesPropertyKey.DependencyProperty;
我使用 XAML 中的此属性来调用它:
<chart:Chart>
<chart:Chart.Series>
<chart:ChartSerie Data="{Binding ChartData1}"/>
<chart:ChartSerie Data="{Binding ChartData2}" />
</chart:Chart.Series>
</chart:Chart>
ChartSerie 代码只是 ChartData 的容器:
public static DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(ObservableCollection<ChartPoint>), typeof(ChartSerie), new FrameworkPropertyMetadata(OnDataChanged));
我的问题我面临的问题是 ChartSerie 实例没有填充来自绑定的数据。它们的 Data 属性始终设置为 null。我在视觉工作室的输出中没有绑定错误。
提前致谢。
编辑:经过一番挖掘后,这里似乎存在真正的绑定问题。看起来Chart的DataContext没有被ChartSerie继承。 ChartSerie 是一个 FrameworkElement 以防万一这很重要
I've created a Chart control and after a while I decided to add multiserie capabilities to it. I implemented the feature using a dependence property of type ObservableCollection<ChartSerie>
like this:
private static DependencyPropertyKey SeriesPropertyKey = DependencyProperty.RegisterReadOnly("Series", typeof(ObservableCollection<ChartSerie>), typeof(Chart), new FrameworkPropertyMetadata(new ObservableCollection<ChartSerie>()));
public static DependencyProperty SeriesProperty = SeriesPropertyKey.DependencyProperty;
I use this property from XAML calling it this way:
<chart:Chart>
<chart:Chart.Series>
<chart:ChartSerie Data="{Binding ChartData1}"/>
<chart:ChartSerie Data="{Binding ChartData2}" />
</chart:Chart.Series>
</chart:Chart>
The ChartSerie code is just a container of the ChartData:
public static DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(ObservableCollection<ChartPoint>), typeof(ChartSerie), new FrameworkPropertyMetadata(OnDataChanged));
The problem I'm facing is that the ChartSerie instances are not being populated with the data coming from the binding. Their Data property is always set to null. I have no binding errors at the output of the visual studio.
Thanks in advance.
EDIT: After digging a bit looks like there is a real binding issue here. Looks like the DataContext of Chart is not inherited by ChartSerie. ChartSerie is a FrameworkElement just in case this matters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我记得,您需要定义自己的非泛型集合,而不是公开泛型
ObservableCollection
:如果您不这样做,事情确实会变得非常奇怪。
As I recall, you need to define your own non-generic collection rather than exposing a generic
ObservableCollection<Whatever>
:If you don't do this, things get very weird indeed.