如何使用附加属性绑定XyDataSeries的ObservableCollection

时间:2019-01-16 14:39:27

标签: c# wpf observablecollection attached-properties scichart

我正在使用SciChart创建一个制图应用程序。 我添加了一个图表修改器类,该类允许编辑图表数据,但只能编辑当前显示的数据。我需要扩展此类,以便可以访问每个XyDataSeries的完整ObservableCollection。

我实现了一个附加属性,可以在MainWindow DataContext中绑定该属性,但是每当我运行该应用程序时,该集合在修饰符类中均显示为null。请您指教。谢谢

public class MoveBlockModifier : ChartModifierBase
{

    public static readonly DependencyProperty XyFGDataProperty = DependencyProperty.RegisterAttached("XyFGData", typeof(ObservableCollection<XyDataSeries<double,double>>), typeof(MoveBlockModifier), new FrameworkPropertyMetadata(new ObservableCollection<XyDataSeries<double,double>>()));

    public ObservableCollection<XyDataSeries<double, double>> XyFGData
    {
        get { return (ObservableCollection < XyDataSeries<double, double>>)GetValue(XyFGDataProperty); }
        set { SetValue(XyFGDataProperty, value); }
    }

    public MoveBlockModifier()
    {            
        _ghostSeries = new FastLineRenderableSeries()
        {
            Stroke = Colors.Black,
            DataSeries = editingSeries,
            Name = "GhostSeries",                
            StrokeThickness = 1,
            Opacity = 0.75,
        };          

    }

} 

Public Class MainWindow: Window, INotifyPropertyChanged
{
private ObservableCollection<XyDataSeries<double, double>> _xyFGData;
    public ObservableCollection<XyDataSeries<double, double>> XYFGData
    {
        get { return _xyFGData; }
        set { _xyFGData = value; OnPropertyChanged("XYFGData"); }
    }
}

MainWindow的XAML

   <s:SciChartSurface x:Name="Chart2">  
                <s:SciChartSurface.ChartModifier>                        
                        <local:MoveBlockModifier  FixStart="{Binding FixStart}" FixEnd="{Binding FixEnd}" 
                                                  IsEnabled="{Binding ChartTwoMoveBlockEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                    XyFGData="{Binding XYFGData, Mode=TwoWay}" />
                    </s:ModifierGroup>
                </s:SciChartSurface.ChartModifier>
            </s:SciChartSurface>

1 个答案:

答案 0 :(得分:0)

上述问题似乎不完整/存在一些错误。您提到了一个附加属性,您将其定义为

public static readonly DependencyProperty XyFGDataProperty = DependencyProperty.RegisterAttached("XyFGData", typeof(ObservableCollection<XyDataSeries<double,double>>), typeof(MoveBlockModifier), new FrameworkPropertyMetadata(new ObservableCollection<XyDataSeries<double,double>>()));

    public ObservableCollection<XyDataSeries<double, double>> XyFGData
    {
        get { return (ObservableCollection < XyDataSeries<double, double>>)GetValue(XyFGDataProperty); }
        set { SetValue(XyFGDataProperty, value); }
    }
...

但这不是在WPF中定义附加属性的方法。请遵循how to register an attached property的MSDN文档。

第二,您在FrameworkPropertyMetadata中定义new ObservableCollectionXyDataSeries<double, double>的默认值,但这是一个坏主意,因为您将在MoveBlockModifier的所有实例之间静态共享ObservableCollectionXyDataSeries<double, double>的一个实例。看看Where to initialize reference type dependency properties for a custom control?

最后,它是您要定义的附加属性,但是在XAML中,您没有像附加属性那样使用它。

此部分:

不正确。 See how an attached property is attached in XAML here

最后,您将MoveBlockModifier.XyFGData绑定到主窗口中的XYFGData属性,但是MoveBlockModifier的DataContext可能不是MainWindow。

我建议重新开始并修复这些错误!