ObservableCollection 作为 DependenceProperty 和绑定问题

发布于 2024-12-10 16:16:01 字数 1260 浏览 9 评论 0原文

我创建了一个图表控件,过了一段时间我决定向它添加多系列功能。我使用 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 技术交流群。

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

发布评论

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

评论(1

世俗缘 2024-12-17 16:16:01

我记得,您需要定义自己的非泛型集合,而不是公开泛型 ObservableCollection

public class ChartSeriesCollection : ObservableCollection<ChartSeries>
{
}

...

public static readonly DependencyProperty ChartSeriesProperty = DependencyProperty.Register(
    "ChartSeries",
    typeof(ChartSeriesCollection),
    typeof(MyClass));

如果您不这样做,事情确实会变得非常奇怪。

As I recall, you need to define your own non-generic collection rather than exposing a generic ObservableCollection<Whatever>:

public class ChartSeriesCollection : ObservableCollection<ChartSeries>
{
}

...

public static readonly DependencyProperty ChartSeriesProperty = DependencyProperty.Register(
    "ChartSeries",
    typeof(ChartSeriesCollection),
    typeof(MyClass));

If you don't do this, things get very weird indeed.

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