C# ListView数据绑定
我正在编写简单的C# ListView数据绑定,c#,wpf,xaml,listview,C#,Wpf,Xaml,Listview,我正在编写简单的WPF应用程序,我想使用ListView来显示项目的List。我的代码是: WPF.xaml MyViewModel.cs OwnedCollection.cs 公共列表元素描述{get;set;} 我100%确信,View和ViewModel之间的通信是正确的,因为显示简单的消息不会给我带来麻烦。我是否在列表视图中进行了正确的绑定?有几件事: 首先, TextBlock Text="{Binding ElementDescriptions}" 没有什么意义,因为Elemen
WPF应用程序
,我想使用ListView
来显示项目的List
。我的代码是:
WPF.xaml
MyViewModel.cs
OwnedCollection.cs
公共列表元素描述{get;set;}
我100%确信,View
和ViewModel
之间的通信是正确的,因为显示简单的消息不会给我带来麻烦。我是否在列表视图中进行了正确的绑定?有几件事:
首先,
TextBlock Text="{Binding ElementDescriptions}"
没有什么意义,因为ElementDescriptions是一个集合。如果要循环查看列表中的所有ElementDescriptions,则应该将ListView的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以便视图知道要更新?这里有更多信息:这一行ItemsSource=“{Binding MyCollection.Elements}
当您将其更改为ItemsSource=“{Binding MyCollection}
时会发生什么?还想知道OwnedCollection是否应该改为ObservaleCollection..没有-列表仍然为空。还是没什么。我已将列表更改为ObservableCollection。这有帮助吗?很抱歉给你带来了混乱
public OwnedCollection Elements { get; set; }
public List<ElementDescriptions> ElementDescriptions { get; set; }
TextBlock Text="{Binding 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>