WPF绑定tp ViewModel

时间:2012-07-06 12:02:40

标签: c# .net xaml data-binding mvvm

我无法将listview绑定到ViewModel。我的ViewModel的代码如下:

class MyViewModel : INotifyPropertyChanged
{
    ObservableCollection<MyDataItemViewModel> dataToShow;

    public ObservableCollection<MyDataItemViewModel> DataToShow
    {
        get
        {
            return dataToShow;  // A breakpoint here is never hit
        }
    }

    public MyViewModel(ObservableCollection<MyDataItemViewModel> toShow)
    {
        dataToShow = toShow;   // A breakpoint here reveals that there is data

        RegisterCommands();
    }
...

我的XAML如下:

    <ListView   Name="DataView"
                ...
                ItemsSource="{Binding MyViewModel.DataToShow}">

        <ListView.View>
            <GridView>
                <GridViewColumn Header="Number"
                        ...
                        DisplayMemberBinding="{Binding Path=Details.Number}" />
                ...

MyDataItemViewModel类的代码返回一个名为Details的公共类,其中包含许多属性,包括Number

屏幕显示正常,但没有任何数据。

1 个答案:

答案 0 :(得分:1)

试试这个:

ItemsSource="{Binding DataToShow}"