WPF绑定问题

时间:2014-12-11 02:23:55

标签: c# wpf

我正在使用WPF工具包的图表,但我遇到了将其绑定到我的ViewModel的问题。什么都没有出现。如果你想知道我有MainWindow.DataContext = MainWindowViewModel。这就是我所拥有的:

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 个答案:

答案 0 :(得分: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));
    }
}