【发布时间】:2014-08-09 21:44:53
【问题描述】:
我有一个LineSeries,我在 WPFToolkit 中使用它。以下是我的 XAML 代码:
<Window x:Class="WpfChartExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chrt="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<chrt:Chart x:Name="simChart" Title="Simulation">
<chrt:LineSeries IndependentValueBinding="{Binding Name}"
DependentValueBinding="{Binding Value}"
ItemsSource="{Binding}">
</chrt:LineSeries>
</chrt:Chart>
<Button Name="btnStart" Width="Auto" Height="30" Grid.Row="1" HorizontalAlignment="Right" Margin="10" Click="btnStart_Click">Start Simulation</Button>
</Grid>
这是生成数据的类:
public partial class MainWindow : Window
{
ObservableCollection<ChartData> chartData;
ChartData objChartData;
Thread thSim;
bool thLock = true;
public MainWindow()
{
InitializeComponent();
chartData = new ObservableCollection<ChartData>();
simChart.DataContext = chartData;
}
public void StartChartDataSimulation()
{
int i = 0;
while (i < 10)
{
chartData.Add(new ChartData() { Name = DateTime.Now.ToString("HH:MM:ss"), Value = new Random().NextDouble() });
Thread.Sleep(1000);
i++;
}
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
StartChartDataSimulation();
}
}
public class ChartData : INotifyPropertyChanged
{
string _Name;
double _Value;
#region properties
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
public double Value
{
get
{
return _Value;
}
set
{
_Value = value;
OnPropertyChanged("Value");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
图表会在所有值生成后更新。因此,该图表一直是空白的。我希望图表在添加值后立即更新。我该怎么做?
【问题讨论】:
标签: c# wpf wpftoolkit