更新和刷新 wpf 图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12725264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update and refresh wpf chart
提问by Michele Cappannari
I'm working with chart of wpf toolkit, and I can't view the inserted data of the chart... Here is the xaml part:
我正在使用wpf工具包的图表,我无法查看图表的插入数据......这是xaml部分:
<chartingToolkit:Chart Height="352" HorizontalAlignment="Left" Name="profilo_cale_chart" Title="Profili cale" VerticalAlignment="Bottom" Width="655" FontSize="16" Margin="252,0,0,55" Visibility="Hidden">
<chartingToolkit:AreaSeries DependentValuePath="Value" IndependentValuePath="Key" IsSelectionEnabled="True" ItemsSource="{Binding}" Foreground="#FF242424" Background="LightSteelBlue" />
</chartingToolkit:Chart>
And the cs: Declaration
和 cs: 宣言
public partial class MainWindow : Window
{ ...
List<KeyValuePair<DateTime, double>> valueList = new List<KeyValuePair<DateTime, double>>();
...
Module called when a user chose two dateTime and press a button
当用户选择两个日期时间并按下按钮时调用的模块
private void refresh_charts(DateTime dtStart, DateTime dtEnd)
{
valueList.Clear();
using (ModelFoosContainer mc1 = new ModelFoosContainer())
{
var res_mea = (from mea in mc1.MeasureSet where (mea.datetime>= dtStart && mea.datetime <= dtEnd) orderby mea.datetime descending select mea).Take(1000);
foreach (Measure mea1 in res_mea){
valueList.Add(new KeyValuePair<DateTime,double>(mea1.datetime, Convert.ToDouble(mea1.depth)));
}
}
profilo_cale_chart.DataContext = valueList;
//I've tried with this two but doesn't work..
//profilo_cale_chart.Refresh();
//this.UpdateLayout();
}
After this the chart has the right and not empty DataContext but doesn't show the values... anyone knows how can I fix it?
在此之后,图表具有正确的而不是空的 DataContext 但不显示值......有人知道我该如何修复它?
采纳答案by Danilo Vulovi?
Try to set name for chart series:
尝试为图表系列设置名称:
<chartingToolkit:AreaSeries Name="Example" DependentValuePath="Value" IndependentValuePath="Key" IsSelectionEnabled="True" ItemsSource="{Binding}" Foreground="#FF242424" Background="LightSteelBlue" />
and after that, set DataContext for AreaSeries:
之后,为 AreaSeries 设置 DataContext:
Example.DataContext = valueList;
回答by Evgeniy Dovgal
Try change "Visibility" for Chart in xaml from
尝试在 xaml 中更改图表的“可见性”
Visibility="Hidden"
to
到
Visibility="Visible"

