在TabControl中为图表使用DataTemplate时出错

时间:2017-11-30 13:19:54

标签: wpf charts datatemplate tabcontrol wpftoolkit

我使用C#,wpf,WPFToolkit。我创建了TabControl。选项卡包含图表和其他信息。我使用模板和选择器。

XAML:

xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
...

<DataTemplate x:Key="ChartTemplate">
   <charting:Chart>
    <charting:LineSeries ItemsSource="{Binding ChartVM}" DependentValuePath="I" IndependentValuePath="Grade"    
                                     Title="Chart">
    </charting:LineSeries>
    <charting:Chart.Axes>
      <charting:LinearAxis Orientation="X" Title="Value_X" ShowGridLines="True"/>
      <charting:LinearAxis Orientation="Y" Title="Value_Y"/>
    </charting:Chart.Axes>
   </charting:Chart> 
</DataTemplate>

<DataTemplate x:Key="TextTemplate">
    <TextBlock Text="Test" />
</DataTemplate>

...

<TabControl ItemsSource="{Binding TabList}">
   <TabControl.ItemTemplate>
        <DataTemplate>
            <HeaderedContentControl Header="{Binding TabName}"/>
        </DataTemplate>
   </TabControl.ItemTemplate>
   <TabControl.ContentTemplateSelector>
       <extension:TabTemplateSelector 
            ChartTemplate="{StaticResource ChartTemplate}"
            TextTemplate="{StaticResource TextTemplate}"/>
   </TabControl.ContentTemplateSelector>
</TabControl>

选择器:

public DataTemplate ChartTemplate { get; set; }
public DataTemplate TextTemplate { get; set; }

public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        TabViewModel tab = (TabViewModel)item;

        if (tab.IsChart) return ChartTemplate;
            return TextTemplate;
    }

视图模型:

List<TabViewModel> tabList = new List<TabViewModel>();
tabList.Add(new TabViewModel(IsChart=true));
tabList.Add(new TabViewModel(IsChart=false));
....
TabList = new ListCollectionView(tabList);

项目运行没有错误。该图正确构建。但如果您切换标签,则会收到错误消息:

    Exception Type: System.InvalidOperationException
    Exception: Cannot determine the size of an axis without an orientation.
    Additional information: 
      System.Object: 
    Stack Trace: 
       at 
    System.Windows.Controls.DataVisualization.Charting.DisplayAxis.GetLength(Size availableSize)
   at System.Windows.Controls.DataVisualization.Charting.DisplayAxis.get_ActualLength()
   at 
  System.Windows.Controls.DataVisualization.Charting.RangeAxis.UpdateActualRange()
   at System.Windows.Controls.DataVisualization.Charting.RangeAxis.System.Windows.Controls.DataVisualization.Charting.IRangeConsumer.RangeChanged(IRangeProvider usesRangeAxis, Range`1 range)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.UpdateActualIndependentAxis()
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.<>c__DisplayClass33.<OnDataPointsChanged>b__31()
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.<>c__DisplayClass1c.<InvokeOnLayoutUpdated>b__1b(Object , EventArgs )
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   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.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(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)

P.S。如果注释掉以下行,则在选项卡之间切换时不会出现错误:

<charting:Chart.Axes>
  <charting:LinearAxis Orientation="X" Title="Value_X" ShowGridLines="True"/>
  <charting:LinearAxis Orientation="Y" Title="Value_Y"/>
</charting:Chart.Axes>

但我需要图表的轴签名。

0 个答案:

没有答案