【问题标题】:Wpf Toolkit Line Chart not displaying dataWpf Toolkit折线图不显示数据
【发布时间】: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


【解决方案1】:

我猜问题是图表不知道如何显示您的 Nullable 值(DateTime?,十进制?)。您可能需要一个转换器来从 Nullable 装饰器中提取值。

这是我的例子: MainWindow.xaml

<Window x:Class="WpfToolkitChart.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="1031" Width="855" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="0,-28,0,28">
        <Grid Height="921">

            <chartingToolkit:Chart  Name="lineChart" Title="Line Series Demo" VerticalAlignment="Top" Margin="33,611,440,0" Height="254">
                <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"/>
            </chartingToolkit:Chart>
        </Grid>
    </ScrollViewer>

</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfToolkitChart
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      showColumnChart();
    }

    public ObservableCollection<KeyValuePair<string, int?>> valueList = new ObservableCollection<KeyValuePair<string, int?>>();

    private void showColumnChart()
    {

      valueList.Add(new KeyValuePair<string, int?>("Developer",60));
      valueList.Add(new KeyValuePair<string, int?>("Misc", 20));
      valueList.Add(new KeyValuePair<string, int?>("Tester", 50));
      valueList.Add(new KeyValuePair<string, int?>("QA", 30));
      valueList.Add(new KeyValuePair<string, int?>("Project Manager", 40));

      //Setting data for line chart
      lineChart.DataContext = valueList;
    }

  }
}

【讨论】:

  • 嗨,我尝试了一个快速而肮脏的解决方案,将其隐式转换为不可为空的值(因为我知道当前值不为空),但这不起作用。
  • 你的绑定有问题:DataContext={Binding} 和 ItemsSource="{Binding LineChartValues}" 对我来说也很奇怪。 LineChartValues 在哪里声明?是在 ViewModel 中还是在 View 中?
  • 在视图模型中。 DataContext 应该从 xaml Binding 本身继承,对吗?所以我可以把它留在图表声明中。
  • 试试这个:DataContext="{Binding LineChartValues}" 和 ItemsSource="{Binding}"。我有一个工作示例,其中列表在视图中声明并直接绑定到图表数据上下文。
  • 也不起作用(我想我已经尝试过了)。如果这样做,我会收到此错误:System.Windows.Data 错误:3:找不到提供 DataContext 的元素。 BindingExpression:Path=LineChartValues;数据项=空;目标元素是'LineSeries'(名称='');目标属性是“DataContext”(类型“对象”)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多