2011-10-06 87 views
0

我为图表使用Wpf Toolkit,并且我意识到,当我将它作为第二个Tabitem时它不起作用。可能是什么问题呢?图表适用于第一个tabitem,但不适用于第二个tabitem?

这是我的图:

<TabControl> 
    <TabItem Header="PowerFlow"> 
    </TabItem> 
    <TabItem Header="Graph" Name="Graphs"> 
     <ScrollViewer HorizontalScrollBarVisibility="Auto" 
VerticalScrollBarVisibility="Auto" Margin="0,-28,0,28"> 
      <Grid Height="921" Background="DarkGray"> 
       <chartingToolkit:Chart Name="lineChart" Title="Power Graph" Background="YellowGreen" Foreground="DarkBlue"     
       VerticalAlignment="Top" Margin="16,36,20,0" Height="432" IsEnabled="True" > 
        <chartingToolkit:LineSeries Title="SolarCell"  
    ItemsSource="{Binding}" DependentValueBinding="{Binding Path=Value}" 
    IndependentValueBinding="{Binding Path=Key}" 
     IsSelectionEnabled="True" DataContext="{Binding}"> 
         <chartingToolkit:LineSeries.DependentRangeAxis> 
          <chartingToolkit:LinearAxis Orientation="Y" Title="Power (W)"></chartingToolkit:LinearAxis> 
         </chartingToolkit:LineSeries.DependentRangeAxis> 
        </chartingToolkit:LineSeries> 
       </chartingToolkit:Chart> 
       <Button Content="Refresh" Height="23" HorizontalAlignment="Left" Margin="718,391,0,0" Name="button1" VerticalAlignment="Top" Width="75" /> 
       <TextBox Height="23" HorizontalAlignment="Left" Margin="696,73,0,0" Name="textBox7" VerticalAlignment="Top" Width="97" Loaded="textBox7_Loaded" /> 
       <Label Content="Time started:" Height="28" HorizontalAlignment="Left" Margin="606,73,0,0" Name="label1" VerticalAlignment="Top" Width="84" /> 
      </Grid> 
     </ScrollViewer> 
    </TabItem> 
</TabControl> 
</Window> 

和后面的代码:

public partial class MainWindow : Window 
{ 
    ObservableCollection<KeyValuePair<double, double>> Power = new ObservableCollection<KeyValuePair<double, double>>(); 
    double i = 0; 

    public MainWindow() 
    { 

     InitializeComponent(); 
     TabItem Graphs = new TabItem(); 
     DispatcherTimer timer = new DispatcherTimer(); 
     timer.Interval = new TimeSpan(0, 0, 1); // per 5 seconds, you could change it 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.IsEnabled = true; 
    } 



    Random random = new Random(DateTime.Now.Millisecond); 
    void timer_Tick(object sender, EventArgs e) 
    { 

     Power.Add(new KeyValuePair<double, double>(i, random.NextDouble())); 
     i += 5; 
     lineChart.DataContext = Power; 

    } 

} 
} 
+0

您可以定义当你把它在第二个选项卡项会发生什么? “不起作用”可能意味着任何数量的东西 – Rachel

回答

2

如果从供LineSeries删除行

DataContext="{Binding}" 

它应该工作。 不幸的是我无法解释它为什么不起作用。但是由于DataContexts通常是在控制层次中派生的,所以在这一点上并不是必需的,并且以某种方式导致这个奇怪的问题。

HTH

+0

如果我从LineSeries中删除该行,即使在第一个tabitem中,它也无法工作。不过谢谢。 –

+0

嗯奇怪,在我的情况下它的工作正常。 只要我删除线,图表绘制得很好。 SvenG

相关问题