简体   繁体   中英

How to Bind an ObservableCollection of XyDataSeries using an Attached Property

I am creating a Charting application using SciChart. I have added a chart modifier class which allows editing of the chart data but only the data currently displayed. I need to extend this class so that the full ObservableCollection of each XyDataSeries can be accessed.

I have implemented an attached property which I can bind to in the MainWindow DataContext however whenever I run the application the collection is showing as null in the modifier class. Please can you advise. Thanks

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"); }
    }
}

XAML of MainWindow

   <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>

The question above seems incomplete / has some errors. You mention an attached property, which you define as this

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); }
    }
...

but this isn't the way to define attached properties in WPF. Follow the MSDN documentation for how to register an attached property .

Secondly, you define a default value of new ObservableCollectionXyDataSeries<double, double> in your FrameworkPropertyMetadata, but this is a bad idea, because you will share one instance of ObservableCollectionXyDataSeries<double, double> statically across all instances of MoveBlockModifier. Have a look at Where to initialize reference type dependency properties for a custom control?

Lastly its an attached property that you want to define but in XAML you are not using it like an attached property.

This part:

is incorrect. See how an attached property is attached in XAML here .

Finally you bind MoveBlockModifier.XyFGData to a property XYFGData in your main window but the DataContext of the MoveBlockModifier might not be MainWindow.

I suggest starting again and fixing these errors!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM