Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView数据绑定_C#_Wpf_Xaml_Listview - Fatal编程技术网

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>