WPF ToolKit チャートコントロールのデータソースを動的に更新するにはどうすればよいですか? 次の例では、TextBlock.Text プロパティを {Binding SomeText} で正常に更新し、MainWindow の DataContext をプロパティ Input に設定しています。(以下のコードを参照してください)
TextBlock.Text は Input.SomeText にバインドされ、チャートは Input.ValueList をデータソースとして使用することを想定しています。
ただし、チャートは空のままです。置くだけで一度に満たすことができます
lineChart.DataContext = Input.ValueList;
メイン ウィンドウ コンストラクターで、XAML のバインディングを ItemsSource="{Binding}" に設定します。ただし、これは起動時にのみ機能し、たとえばボタンをクリックしても更新されません。アプリケーションが新しい受信データで実行されている間にチャートを更新したいと考えています。
次の 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}" />
コード付き:
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 が更新され、ValueList が変更されたことを確認するために、ValueList.Count をテキストブロックに配置すると、カウントが正常に増加していることがわかりますが、チャートは同じままです。
したがって、これにより1つの成功したバインディングが発生します(ただし、更新されません):
lineChart.DataContext = Input.ValueList;
ItemsSource="{Binding}"
これはまったくバインドしません:
ItemsSource="{Binding ValueList}"