C#SciChart"无法向< ...>"类型的对象添加内容错误

时间:2017-04-03 07:48:11

标签: c# mvvm caliburn.micro scichart

我想在AnnotationCollection类中添加一个参数。因为它是sealed,所以我不能使用继承创建一个新类。我改为创建了一个新类:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Schema;
using System.Xml;
using System.Xml.Serialization;
using System.Windows;
using SciChart.Charting.Visuals.Annotations;
using SciChart.Charting.Visuals.RenderableSeries;
using SciChart.Charting.Model.ChartData;
using SciChart.Charting.Visuals;

namespace ChartsDisplayer2016.Core.Charts.Models
{

    public sealed class ModifiedAxisMarkerAnnotations : ObservableCollection<IAnnotation>, IXmlSerializable
    {
        public static readonly DependencyProperty AnnotationsSourceProperty = 
            DependencyProperty.Register("AnnotationsSource", typeof(IEnumerable<SeriesInfo>), 
                typeof(ModifiedAxisMarkerAnnotations), 
                new PropertyMetadata(null, OnAnnotationsSourceChanged));

        public ISciChartSurface ParentSurface { get; set; }
        private IEnumerable<SeriesInfo> _annotationsSource;
        public IEnumerable<SeriesInfo> AnnotationsSource
        {
            get => _annotationsSource;
            set { _annotationsSource = value; RebuildAnnotations(); }
        }
        private AnnotationCollection x;
        // Get a notification when new labels are set.
        private static void OnAnnotationsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        // Recreate all annotations
        private void RebuildAnnotations()
        {
           //some stuff
        }
        public XmlSchema GetSchema()
        {
            return new XmlSchema();
        }
        public void ReadXml(XmlReader reader)
        { }
        public void WriteXml(XmlWriter writer)
        { }
    }
}

并尝试在xaml中使用它:

<UserControl x:Class="ChartsDisplayer2016.Core.Charts.Views.ChartUniversalView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ChartsDisplayer2016.Core.Charts.Views"
             xmlns:localmodel="clr-namespace:ChartsDisplayer2016.Core.Charts.Models"
             mc:Ignorable="d" 
             xmlns:s="http://schemas.abtsoftware.co.uk/scichart">

    <Grid Width="Auto"                            
          FocusManager.FocusedElement="{Binding ElementName=MainChartSurface}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>


        <s:SciChartSurface  RenderableSeries="{Binding RenderableSeries}"
                            YAxes="{Binding ChartYAxes, Mode=TwoWay}" >

            <!--  Here we use it  -->
            <localmodel:ModifiedAxisMarkerAnnotations AnnotationsSource="{Binding rolloverModifier.SeriesData.SeriesInfo}"/>

            <!--  Specify interactivity modifiers  -->
            <s:SciChartSurface.ChartModifier>
                <s:ModifierGroup >
                    <s:RolloverModifier Width="3" Height="3" ShowTooltipOn="MouseRightButtonDown" 
                                        Name="rolloverModifier"/>
                </s:ModifierGroup>
            </s:SciChartSurface.ChartModifier>
        </s:SciChartSurface>
    </Grid>
</UserControl>

编译器抛出错误:

  

无法将内容添加到&#39; SciChart.Charting.Visuals.SciChartSurface&#39;。

类型的对象

如何在ModifiedAxisMarkerAnnotations对象中使用我的SciChartSurface类?

1 个答案:

答案 0 :(得分:0)

我担心你要做的事情是不可能的,因为SciChartSurface.Annotations属性需要AnnotationCollection的类型。由于无法继承AnnotationCollection,因此必须使用原始注释集合而不是派生类型或注释的ObservableCollection。

我建议你做的是创建一个附加属性或附加行为,它允许你绑定到IEnumerable<SeriesInfo>,然后从绑定值创建注释,而不是覆盖集合类型。