Целью является создание переменного количества строк в таблице WPF. Я пытаюсь создать метод в C#, который будет создавать ряд линий, а затем связать его с определенным индексом в List<Collection<Point>>
Binding LineSeries с индексом
В 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
, чтобы действовать как индексы для моего привязки (LineGraphDataPoints будет Dictionary<Guid, Collection<Point>>
); однако я готов использовать индексы int
, если это упростит логику.
Обновление: XAMLParseException Подробности: Исключение с binding.Path = new PropertyPath("(0)[(1)]", LineGraphDataPoints, bindingIdentifier);
uncommented.
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]);
uncommented.
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>>
.
Нет смысла рассказывать нам, что вы получаете «XAMLParseException», если вы не сообщите нам, что его детали ... убедитесь, что вы также смотрите во внутренних исключениях, потому что обычно это происходит, когда ваш фактический «Исключение» найден. – Sheridan
Обновлено для включения сведений об исключении для обеих строк binding.Path. – Reid
Как говорится в «Исключении», вы не можете использовать «Point» в качестве параметра accessor для «PropertyPath». – Sheridan