简体   繁体   English

使用MVVM将数据绑定到WPF中的ListView控件

[英]Bind data to the ListView control in WPF using MVVM

I have tried to bind the data into the ListView control: 我试图将数据绑定到ListView控件:

<ListView Margin="8" Height="400" Width="650" 
    ItemsSource="{Binding Path=MyData}">
    <ListView.View>
         <GridView>
             <GridViewColumn Header="ID" Width="Auto" 
                  DisplayMemberBinding="Binding Path=ID}" >
             </GridViewColumn>
             <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" 
                  Header="Name" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Path=Price}" 
                  Header="Price" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Path=Author}" 
                  Header="Author" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Path=Catalog}" 
                  Header="Catalog" Width="100"/>
           </GridView>
    </ListView.View>
</ListView>

  ObservableCollection<TableInfo> _MyData
  public ObservableCollection<TableInfo> MyData{ get; set; }

However, it's not display anything in the window but MyData is an ObservableCollection . 但是,它不会在窗口中显示任何内容,但MyData是一个ObservableCollection How do I bind the data? 如何绑定数据?

Tanya, 蔡健雅,

If you set your view model properly and if you're sure that your MyData collection is not null or empty, try to remove the "Path" keywords from your xaml: 如果您正确设置了视图模型,并且如果您确定MyData集合不为null或为空,请尝试从xaml中删除“Path”关键字:

<ListView Margin="8" Height="400" Width="650" 
    ItemsSource="{Binding Path=MyData}">
    <ListView.View>
         <GridView>
             <GridViewColumn Header="ID" Width="Auto" 
                  DisplayMemberBinding="{Binding ID}" >
             </GridViewColumn>
             <GridViewColumn DisplayMemberBinding="{Binding Name}" 
                  Header="Name" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Price}" 
                  Header="Price" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Author}" 
                  Header="Author" Width="100"/>
             <GridViewColumn DisplayMemberBinding="{Binding Catalog}" 
                  Header="Catalog" Width="100"/>
           </GridView>
    </ListView.View>
</ListView>

If this doesn't help, check the debug output and post it, we will definitely figure something out. 如果这没有帮助,检查调试输出并发布它,我们肯定会想出办法。

Your XAML looks fine to me. 你的XAML看起来很好。

Make sure you are setting the DataContext correctly. 确保正确设置DataContext

yourView.DataContext = YouViewModel;

When the Binding is first evaluated, it may (depending on your code) find null. 首次评估Binding时,它可能(取决于您的代码)找到null。 It does not get notified if you later set it to something sensible. 如果您稍后将其设置为合理的,则不会收到通知。

Try to make sure that you create the ObservableCollection in your classes costructor and not overwrite it somewhere else. 尝试确保在类costructor中创建ObservableCollection,而不是在其他地方覆盖它。 You could alternativly implement INotifyPropertyChanged and raise a PropertyChanged event on te MyData setter. 您可以替代地实现INotifyPropertyChanged并在te Myset setter上引发PropertyChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM