图形工作在第一个tabitem,但不是第二个tabitem? [英] Graph works on first tabitem but not second tabitem?

查看:126
本文介绍了图形工作在第一个tabitem,但不是第二个tabitem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Wpf工具包作为图形,我意识到它不工作,当我把它作为第二个tabitem。这可能是什么问题?

I am using Wpf Toolkit for the graph, and I realise that it doesn't work when I place it as the second tabitem. What could be the problem?

这是我的图表:

<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;

    }

}
}


推荐答案

如果删除此行

DataContext="{Binding}"

从LineSeries它应该工作。
不幸的是,我不能解释为什么它不工作。但是由于DataContext通常是在控制层次中派生的,因此在这一点上没有必要,而且会以某种方式导致这个奇怪的问题。

from the LineSeries it should work. Unfortunately I can't explain why it isn't working. But as DataContexts are usually derived in the control hierarchy it isn't necessary at this point and somehow leads to this strange issue.

HTH

这篇关于图形工作在第一个tabitem,但不是第二个tabitem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆