【发布时间】:2016-04-22 07:49:57
【问题描述】:
我目前正在尝试使用 wpf 工具包向我的应用程序添加折线图。
图表本身显示没有问题,但数据不显示。 如果我设置一个断点来查看数据,我可以清楚地看到,Observable Collection 填充了所需的数据。
这是我的 XAML 中的图表:
<charting:Chart x:Name="Gehtdas" Grid.Row="1" Width="Auto" Height="Auto">
<charting:Chart.Style>
<Style>
<Setter Property="charting:Chart.Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=TypeSelection, Path=SelectedItem}" Value="Line_Chart">
<Setter Property="charting:Chart.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</charting:Chart.Style>
<charting:Chart.Axes>
<charting:LinearAxis Title="Tyres" Orientation="Y" Interval="100" Visibility="Visible" Maximum="1000" Minimum="0"/>
<charting:DateTimeAxis x:Name="TimeAxis" Title="Time" Orientation="X"/>
</charting:Chart.Axes>
<charting:LineSeries DataContext="{Binding}" Title="Tyres per Interval" DependentValueBinding="{Binding Path=Value}"
IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding LineChartValues}"
IsSelectionEnabled="True" AnimationSequence="FirstToLast" />
</charting:Chart>
LineChartValues 是这样的 KeyValuePair Observable Collection。
private ObservableCollection<KeyValuePair<DateTime?, decimal?>> _lineChartValues;
public ObservableCollection<KeyValuePair<DateTime?, decimal?>> LineChartValues
{
get
{
return _lineChartValues;
}
set
{
_lineChartValues = value;
}
}
Viewmodel 通过 ISite 绑定到 xaml。
谁能告诉我我做错了什么? 我现在非常绝望,非常感谢任何帮助。
编辑: 如果我删除 DataContext={Binding} 部分,我会在填充 Observable 集合时得到 NullReferenceException。
EDIT2:加载的窗口中的值填充: Xaml:
<charting:Chart x:Name="Gehtdas" Grid.Row="1" Width="Auto" Height="Auto">
<!--<charting:Chart.Style>
<Style>
<Setter Property="charting:Chart.Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=TypeSelection, Path=SelectedItem}" Value="Line_Chart">
<Setter Property="charting:Chart.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</charting:Chart.Style>-->
<charting:Chart.Axes>
<charting:LinearAxis Title="Tyres" Orientation="Y" Interval="100" Visibility="Visible" Maximum="1000" Minimum="0"/>
<charting:DateTimeAxis x:Name="TimeAxis" Title="Time" Orientation="X"/>
</charting:Chart.Axes>
<charting:LineSeries Title="Tyres per Interval" DependentValueBinding="{Binding Path=Value}"
IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding}"
IsSelectionEnabled="True"/>
</charting:Chart>
代码隐藏:
public ObservableCollection<KeyValuePair<DateTime, int?>> valueList = new ObservableCollection<KeyValuePair<DateTime, int?>>();
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// workaround for Bug in DateTimeAxis which causes the Axis to divide the Default IntervalType of Year into given IntervalType and Interval.
//Setting the IntervalType and Interval this way, prevents it from calculating the interval for a whole year.
this.TimeAxis.Minimum = new DateTime(2016, 02, 10, 0, 0, 0);
this.TimeAxis.Maximum = new DateTime(2016, 02, 10, 16, 0, 0);
//this.TimeAxis.Minimum = DateTime.Now.Date;
//this.TimeAxis.Maximum = DateTime.Now;
this.TimeAxis.IntervalType = System.Windows.Controls.DataVisualization.Charting.DateTimeIntervalType.Hours;
this.TimeAxis.Interval = 1;
valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 10, 0, 0), 60));
valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 11, 0, 0), 40));
valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 12, 0, 0), 50));
valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 13, 0, 0), 30));
valueList.Add(new KeyValuePair<DateTime, int?>(new DateTime(2016, 02, 10, 14, 0, 0), 40));
Gehtdas.DataContext = valueList;
}
编辑 3:出现异常堆栈跟踪:
at
System.Windows.Controls.DataVisualization.Charting.LineAreaBaseSeries`1.UpdateDataPoint(DataPoint dataPoint)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointActualIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointActualIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args)
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(Object oldValue, Object newValue)
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnActualIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at System.Windows.Controls.DataVisualization.Charting.DataPoint.set_ActualIndependentValue(Object value)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.OnDataPointIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
at System.Windows.Controls.DataVisualization.Charting.LineAreaBaseSeries`1.OnDataPointIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args)
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(Object oldValue, Object newValue)
at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.TreeWalkHelper.InvalidateTreeDependentProperty(TreeChangeInfo info, DependencyObject d, FrameworkObject& fo, DependencyProperty dp, FrameworkPropertyMetadata fMetadata, Style selfStyle, Style selfThemeStyle, ChildRecord& childRecord, Boolean isChildRecordValid, Boolean hasStyleChanged, Boolean isSelfInheritanceParent)
at System.Windows.TreeWalkHelper.InvalidateTreeDependentProperties(TreeChangeInfo info, FrameworkElement fe, FrameworkContentElement fce, Style selfStyle, Style selfThemeStyle, ChildRecord& childRecord, Boolean isChildRecordValid, Boolean hasStyleChanged, Boolean isSelfInheritanceParent)
at System.Windows.FrameworkElement.InvalidateTreeDependentProperties(TreeChangeInfo parentTreeState, Boolean isSelfInheritanceParent)
at System.Windows.FrameworkElement.OnAncestorChangedInternal(TreeChangeInfo parentTreeState)
at System.Windows.TreeWalkHelper.InvalidateOnTreeChange(FrameworkElement fe, FrameworkContentElement fce, DependencyObject parent, Boolean isAddOperation)
at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
at System.Windows.FrameworkElement.AddLogicalChild(Object child)
at System.Windows.Controls.UIElementCollection.InsertInternal(Int32 index, UIElement element)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.AddDataPoint(DataPoint dataPoint)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.AddObject(Object dataContext)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.LoadDataPoints(IEnumerable newItems, IEnumerable oldItems)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.Refresh()
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.OnDataContextChanged(DependencyObject contextElement)
at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.TreeWalkHelper.OnInheritablePropertyChanged(DependencyObject d, InheritablePropertyChangeInfo info, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.WalkLogicalChildren(FrameworkElement feParent, FrameworkContentElement fceParent, IEnumerator logicalChildren)
at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
at System.Windows.DescendentsWalker`1._VisitNode(DependencyObject d, Boolean visitedViaVisualTree)
at System.Windows.DescendentsWalker`1.WalkFrameworkElementLogicalThenVisualChildren(FrameworkElement feParent, Boolean hasLogicalChildren)
at System.Windows.DescendentsWalker`1.IterateChildren(DependencyObject d)
at System.Windows.DescendentsWalker`1.StartWalk(DependencyObject startNode, Boolean skipStartNode)
at System.Windows.TreeWalkHelper.InvalidateOnInheritablePropertyChange(FrameworkElement fe, FrameworkContentElement fce, InheritablePropertyChangeInfo info, Boolean skipStartNode)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at Mfc.Statistic.StoragePerformance.MainWindow_Loaded(Object sender, RoutedEventArgs e) in d:\Source_Wista\2014-103141 GDC Bridgestone Burgos Materialfluss\MFC-Solution\Mfc\Statistic\StoragePerformance.xaml.cs:line 91
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at Wista.App.Main() in d:\Source_Wista\Wista\Wista\obj\Release\App.g.cs:line 0
EDIT4:
所以我在一个新的空解决方案中尝试了简单的静态数据方法,它在那里工作。我想问题在于工具包和其他一些框架之间的一些冲突。
【问题讨论】:
-
在您的
Style中,您将Chart.Visibility设置为Collapsed。 -
是的,整个图表设置为折叠,但是当我在组合框中选择正确的值时,它会显示出来。正如我所说,问题不在于图表本身,而在于数据没有显示出来。
-
从样式中删除该行后,我会看到图表数据......
-
另外,我在主窗口
Loaded事件上加载视图模型。 -
好吧,这很奇怪,如果我删除该行,我会在填充 Observable Collection 时收到 NullReferenceException...
标签: wpf charts wpftoolkit