绑定LineSeries与索引

时间:2014-01-20 16:46:15

标签: c# wpf xaml binding

目标是在WPF图表中创建可变数量的行。我试图在C#中创建一个方法,创建一个系列,然后将其绑定到List<Collection<Point>>

中的特定索引

使用有限行数的xaml是:

<charting:LineSeries Name="LineSeries1" ItemsSource="{Binding Path=LineGraphDataPoints[0], ElementName=MainControl, Converter={StaticResource JellyConverter}}" IndependentValuePath="X" DependentValuePath="Y" TransitionDuration="0" PolylineStyle="{StaticResource LinePolylineStyle}">
                <charting:LineSeries.DataPointStyle>
                    <Style BasedOn="{StaticResource LineDataPointStyle}" TargetType="charting:LineDataPoint">
                        <Setter Property="Background" Value="Red"/>
                    </Style>
                </charting:LineSeries.DataPointStyle>
            </charting:LineSeries> 

这是我创建的用于在特定索引处生成新LineSeries的方法:

private LineSeries createLineSeries(Brush brush, int bindingIdentifier)
    {

        LineSeries line = new LineSeries();
        line.IndependentValuePath = "X";
        line.DependentValuePath = "Y";
        line.TransitionDuration = new TimeSpan(0L);
        line.PolylineStyle = Resources["LinePolylineStyle"] as Style;
        Style style = new Style(typeof(LineDataPoint), Resources["LineDataPointStyle"] as Style);
        Setter backgroundSetter = new Setter(Control.BackgroundProperty, brush);
        style.Setters.Add(backgroundSetter);
        line.DataPointStyle = style;

        Binding binding = new Binding();
        //binding.Path = new PropertyPath("(0)[(1)]", LineGraphDataPoints, bindingIdentifier);
        //binding.Path = new PropertyPath(LineGraphDataPoints[bindingIdentifier]);
        binding.ElementName = "MainControl";
        binding.Converter = Resources["JellyConverter"] as IValueConverter;
        line.SetBinding(LineSeries.ItemsSourceProperty, binding);


        return line;
    }

问题是ItemSourceProperty的绑定路径在运行时继续失败,给我一个XAMLParseException。

如果可能,我想使用Guid个对象作为我的Binding的索引(LineGraphDataPoints将是Dictionary<Guid, Collection<Point>>);但是,我愿意使用int索引,如果这样可以使逻辑更简单。

更新: XAMLParseException详细信息: binding.Path = new PropertyPath("(0)[(1)]", LineGraphDataPoints, bindingIdentifier);未注释的例外情况。

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='The invocation of the constructor on type 'RSI.ChartView.JellyCharting' that matches the specified binding constraints threw an exception.' Line number '15' and line position '10'.
  Source=PresentationFramework
  LineNumber=15
  LinePosition=10
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Object 'System.Collections.Generic.List`1[[System.Collections.ObjectModel.Collection`1[[System.Windows.Point, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' cannot be used as an accessor parameter for a PropertyPath. An accessor parameter must be DependencyProperty, PropertyInfo, or PropertyDescriptor.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.PropertyPath.ResolvePropertyName(String name, Object item, Type ownerType, Object context, Boolean throwOnError)
            at System.Windows.PropertyPath.ResolvePathParts(ITypeDescriptorContext typeDescriptorContext)
            at RSI.ChartView.JellyCharting.createLineSeries(Brush brush, Int32 bindingIdentifier) in c:\Projects\rsiphase2\RSI\ChartView\JellyCharting.xaml.cs:line 1557
            at RSI.ChartView.JellyCharting..ctor() in c:\Projects\rsiphase2\RSI\ChartView\JellyCharting.xaml.cs:line 237
       InnerException: 

binding.Path = new PropertyPath(LineGraphDataPoints[bindingIdentifier]);未注释的例外情况。

  System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='The invocation of the constructor on type 'RSI.ChartView.JellyCharting' that matches the specified binding constraints threw an exception.' Line number '15' and line position '10'.
  Source=PresentationFramework
  LineNumber=15
  LinePosition=10
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Object 'System.Collections.ObjectModel.Collection`1[[System.Windows.Point, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' cannot be used as an accessor parameter for a PropertyPath. An accessor parameter must be DependencyProperty, PropertyInfo, or PropertyDescriptor.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.PropertyPath.ResolvePropertyName(String name, Object item, Type ownerType, Object context, Boolean throwOnError)
            at System.Windows.PropertyPath.ResolvePathParts(ITypeDescriptorContext typeDescriptorContext)
            at RSI.ChartView.JellyCharting.createLineSeries(Brush brush, Int32 bindingIdentifier) in c:\Projects\rsiphase2\RSI\ChartView\JellyCharting.xaml.cs:line 1558
            at RSI.ChartView.JellyCharting..ctor() in c:\Projects\rsiphase2\RSI\ChartView\JellyCharting.xaml.cs:line 237
       InnerException: 

更新: LineGraphDataPoints是这里定义的List<Collection<Point>>

public List<Collection<Point>> LineGraphDataPoints
    {
        get { return (List<Collection<Point>>)GetValue(LineGraphDataPointsProperty); }
        private set { SetValue(LineGraphDataPointsProperty, value); }
    }
    public static readonly DependencyProperty LineGraphDataPointsProperty = DependencyProperty.Register("LineGraphDataPointsProperty", typeof(List<Collection<Point>>), typeof(JellyCharting), new UIPropertyMetadata(null));

我想绑定到Collection<Point>中给定索引处存储的整个List<Collection<Point>>

2 个答案:

答案 0 :(得分:0)

您无法将Point类型的单个对象绑定到ItemsSource集合:

ItemsSource="{Binding Path=LineGraphDataPoints[0], ElementName=MainControl ...

如果您仍想在图表上绘制单点 - 您应该将ItemsSource绑定到只有一个项目/点 的集合,或者使用将单个元素“包装”到集合中的转换器

答案 1 :(得分:0)

如异常所述,您不能使用Point作为PropertyPath的访问者参数。用简单的英语,这意味着您无法使用Point作为Binding.Path属性值...您只能使用DependencyProperty。有关详细信息,请查看Creating a bindable Point in C# WPF

LineGraphDataPoints[0]Collection<Point> ...但这在这里没有任何区别。您只能从DependencyProperty绑定到DependencyObjectPointstructDependencyProperty。所以,即使它在Collection中,也没关系,因为你无法绑定它。

但是,这不一定是故事的结尾。虽然您收到此错误,但可能做错了,所以Exception可能无关紧要。我必须承认对这些图表工具一无所知,但在更常见的WPF集合控件中,我们不需要手动设置集合中每个项目的索引。

除此之外,我们只需定义一个DataTemplate来定义每个项目(或您的Point})应该是什么样子。所以可能就是这样,您需要做的就是定义您的赢了DataTemplate。有关详细信息,请阅读MSDN上的Data Templating Overview page。您可能还需要参考图表控件的文档。