2014-12-11 141 views
0

我正在使用WPF工具包的圖表,但我遇到了將它綁定到ViewModel的問題。什麼都沒有出現。如果你想知道,我有MainWindow.DataContext = MainWindowViewModel。這是我有:WPF綁定問題

MainWindow.xaml

<chartingToolkit:Chart Grid.Row="2"> 
    <chartingToolkit:ColumnSeries Name="line_chart" 
            IndependentValuePath="Key" 
            DependentValuePath="Value" 
            ItemsSource="{Binding Me}"/> 
</chartingToolkit:Chart> 

MainWindowViewModel.cs

class MainWindowViewModel 
{ 
    public List<KeyValuePair<string, int>> Me { get; set; } 

    public MainWindowViewModel(Model model) 
    { 
     this.model = model; 

     me.Add(new KeyValuePair<string, int>("test", 1)); 
     me.Add(new KeyValuePair<string, int>("test1", 1000)); 
     me.Add(new KeyValuePair<string, int>("test2", 20)); 
     me.Add(new KeyValuePair<string, int>("test3", 500)); 
    } 

    Model model; 

    List<KeyValuePair<string, int>> me = new ObservableCollection<KeyValuePair<string,int>>(); 
} 

回答

1

我以前沒有使用過該圖表的工具,但你綁定到一個公共Me屬性對於您要添加值的私人me字段沒有任何參考。

刪除私人領域,並嘗試這個:

class MainWindowViewModel 
{ 
    public ObservableCollection<KeyValuePair<string, int>> Me { get; private set; } 

    public MainWindowViewModel(Model model) 
    { 
     this.model = model; 

     // Instantiate in the constructor, then add your values 
     Me = new ObservableCollection<KeyValuePair<string, int>>(); 

     Me.Add(new KeyValuePair<string, int>("test", 1)); 
     Me.Add(new KeyValuePair<string, int>("test1", 1000)); 
     Me.Add(new KeyValuePair<string, int>("test2", 20)); 
     Me.Add(new KeyValuePair<string, int>("test3", 500)); 
    } 
} 
+0

我是個白癡......甚至沒有趕上。謝謝,完美的作品! – keelerjr12 2014-12-11 02:39:32

+0

沒問題,不客氣。 :) – 2014-12-11 02:43:22