如何動態更新WPF ToolKit chartcontrol的數據源?在下面的示例中,我使用{Binding SomeText}成功更新TextBlock.Text屬性,並將MainWindow的DataContext設置爲屬性Input。 (請參閱下面的代碼)如何將WPF Toolkit圖表的項目源動態綁定到公共屬性?
TextBlock.Text綁定到Input.SomeText,Chart假定使用Input.ValueList作爲數據源。
雖然圖表仍然爲空。我可以放置
lineChart.DataContext = Input.ValueList;
在主窗口構造一旦填充它,並設置在XAML到的ItemsSource「{結合}」的結合=。但是這隻在啓動時才起作用,例如當你點擊一個按鈕時它不會更新。我想在應用程序運行時使用新的傳入數據更新圖表。
我有以下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.Count在文本塊,你可以看到按照它應該上升的趨勢,但是圖表保持不變。
所以這導致1 succesfull綁定(但犯規更新):
lineChart.DataContext = Input.ValueList;
ItemsSource="{Binding}"
這並不是在所有綁定:
ItemsSource="{Binding ValueList}"
使ValueList屬性似乎沒有幫助。我已經閱讀過您分享的鏈接,使用上面提到的方法,當ValueList更新圖表時,我可以綁定數據1次。如上所述,文本框會成功更新。它可能是xaml中的問題什麼是錯誤的? – Ben
我已經將值賦給了一個屬性,並將datacontext設置爲這樣的實例: DataContext = Input; lineChart.DataContext =輸入; 爲什麼它只綁定一次? //編輯:所以ItemsSource =「{Binding ValueList}」只能成功綁定,但只添加1次,添加更多值時不會更新。 – Ben
看到我的編輯2.不過,我認爲你的addvalues方法也有一個問題,因爲你不使用observablecollection – blindmeis