English 中文(简体)
使用MVVER的WPF 清单控制数据
原标题:Bind data to the ListView control in WPF using MVVM
  • 时间:2012-01-11 11:31:10
  •  标签:
  • c#
  • wpf
  • mvvm

我试图将数据输入<代码>。 清单: 控制:

<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; }

但是,它没有在窗户中展示任何东西,但<代码>MyData为。 ObservableCollection。 我如何约束数据?

最佳回答

Tanya,

如果你妥善确定你的观察模式,如果你确信你的MyData收集工作不是无效或空洞的,则试图从你的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.

问题回答

页: 1 对我来说,我会感到 fine。

Make sure you are setting the DataContext correctly.

yourView.DataContext = YouViewModel;

在第一次评估具有约束力的条约时,它可能(根据你的法典)认为无效。 如果你后来将此事通知他,那不会得到通知。

Try to make sure that you create the ObservableCollection in your classes costructor and not overwrite it somewhere else. You could alternativly implement INotifyPropertyChanged and raise a PropertyChanged event on te MyData setter.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签