2013-01-10 106 views
0

我在写简单的WPF Application,我想用ListView来显示List的项目。我的代码是:ListView数据绑定

WPF.xaml

<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.Elements}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding ElementDescriptions}" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

WPF.xaml.cs

public MyViewModel ViewModel 
    { 
     get { return DataContext; } 
     set { DataContext = value; } 
    } 

MyViewModel.cs

public OwnedCollection Elements { get; set; } 

OwnedCollection.cs

public List<ElementDescriptions> ElementDescriptions { get; set; } 

我100%肯定,ViewViewModel之间的通信是正确的,因为显示简单的消息并没有让我烦恼。我在做ListView的正确绑定吗?

+0

此行'的ItemsSource = “{结合MyCollection.Elements}'什么,当你将其更改为'发生的ItemsSource =”{结合MyCollection的}'也想知道如果OwnedCollection应改用的ObservableCollection .. – MethodMan

+0

什么都没有 - 名单仍然是空的。依然没有。 – Fka

+0

我已经将List 更改为ObservableCollection Fka

回答

0

有两件事情:

首先,

TextBlock Text="{Binding ElementDescriptions}" 

不使一个很大的意义,因为ElementDescriptions是一个集合。如果你通过你的列表,你真正应该结合ListView控件来ElementDescriptions的的ItemSource然后访问ElementDescriptions类的一些文字财产全部ElementDescriptions要循环:

<ListView Grid.Column="0" Grid.Row="1" Margin="10,0,10,5" ItemsSource="{Binding MyCollection.ElementsElementDescriptions }"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding ElementDescriptions.SomeTextField}" /> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

其次,你使用INotifyPropertyChanged的这样的观点知道更新?在这里更多信息:OnPropertyChanged with a List