图表适用于第一个选项卡但不适用于第二个选项卡?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除该线路
如果您从 LineSeries 中 ,它应该可以工作。
不幸的是我无法解释为什么它不起作用。但由于 DataContext 通常是在控制层次结构中派生的,因此此时没有必要,并且会以某种方式导致这个奇怪的问题。
华泰
If you remove the line
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