图表适用于第一个选项卡但不适用于第二个选项卡?

发布于 2024-12-08 14:17:58 字数 2773 浏览 0 评论 0原文

我正在使用 Wpf Toolkit 来绘制图表,并且我意识到当我将其放置为第二个 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;

    }

}
}

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?

This is my graph:

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

and the code behind:

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;

    }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-12-15 14:17:58

删除该线路

DataContext="{Binding}"

如果您从 LineSeries 中 ,它应该可以工作。
不幸的是我无法解释为什么它不起作用。但由于 DataContext 通常是在控制层次结构中派生的,因此此时没有必要,并且会以某种方式导致这个奇怪的问题。

华泰

If you remove the line

DataContext="{Binding}"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文