How can you dynamically update the datasource of a WPF ToolKit chartcontrol? In the following example I update the TextBlock.Text property succesfully with {Binding SomeText} and setting the DataContext of the MainWindow to the property Input. (Please see the code below)
TextBlock.Text is binded to Input.SomeText and the Chart is suppose to use Input.ValueList as a datasource.
The chart remains empty though. I can fill it once with placing
lineChart.DataContext = Input.ValueList;
in the Main Window constructor and set the binding in XAML to ItemsSource="{Binding}". But this only works at startup, it doesn't update when you click a button for example. I want to update the chart while the application is running with new incoming data.
I have the following XAML:
<chartingToolkit:Chart Name="lineChart">
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding ValueList}">
</chartingToolkit:LineSeries>
</chartingToolkit:Chart>
<Button Width="100" Height="24" Content="More" Name="Button1" />
<TextBlock Name="TextBlock1" Text="{Binding SomeText}" />
With code:
class MainWindow
{
public DeviceInput Input;
public MainWindow()
{
InitializeComponent();
Input = new DeviceInput();
DataContext = Input;
lineChart.DataContext = Input;
Input.SomeText = "Lorem ipsum.";
}
private void Button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
Input.AddValues();
}
}
public class DeviceInput : INotifyPropertyChanged
{
private string _SomeText;
public string SomeText {
get { return _SomeText; }
set {
_SomeText = value;
OnPropertyChanged("SomeText");
}
}
public List<KeyValuePair<string, int>> ValueList {get; private set;}
public DeviceInput()
{
ValueList = (new List<KeyValuePair<string, int>>());
AddValues();
}
public void AddValues()
{
//add values (code removed for readability)
SomeText = "Items: " + ValueList.Count.ToString();
OnPropertyChanged("ValueList");
}
public event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
SomeText gets updated and to make sure the ValueList changes I place ValueList.Count in the textblock and you can see the count rising as it should, however the Chart remains the same.
So this results in 1 succesfull binding (but doesnt update):
lineChart.DataContext = Input.ValueList;
ItemsSource="{Binding}"
This doesn't bind at all:
ItemsSource="{Binding ValueList}"