2014-12-11 84 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