我在添加显示对象成员的所有值的行时遇到问题。
以下是我设置列表视图的方法:
<ListView Height="178" HorizontalAlignment="Left" Margin="238,31,0,0" Name="SpoolSheetListView" VerticalAlignment="Top" Width="555" HorizontalContentAlignment="Stretch" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Width="85" Header="Column 1" />
<GridViewColumn Width="120" Header="Column 2" />
<GridViewColumn Width="120" Header="Column 3" />
<GridViewColumn Width="120" Header="Column 4" />
<GridViewColumn Width="115" Header="Column 5" />
</GridView>
</ListView.View>
</ListView>
比方说,myObject有5个成员:member1到member5,它们都是string类型。如何在ListView中为该对象添加单行?
再次感谢大家!
答案 0 :(得分:0)
WPF列表控件在与数据绑定一起使用时效果最佳;您应该将ItemsSource
的{{1}}绑定到对象列表,并将每列绑定到这些对象的属性。
ListView
(<ListView ItemsSource="{Binding Items}" Height="178" HorizontalAlignment="Left" Margin="238,31,0,0" Name="SpoolSheetListView" VerticalAlignment="Top" Width="555" HorizontalContentAlignment="Stretch" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Width="85" Header="Column 1" DisplayMemberBinding="{Binding Member1}" />
<GridViewColumn Width="120" Header="Column 2" DisplayMemberBinding="{Binding Member2}" />
<GridViewColumn Width="120" Header="Column 3" DisplayMemberBinding="{Binding Member3}" />
<GridViewColumn Width="120" Header="Column 4" DisplayMemberBinding="{Binding Member4}" />
<GridViewColumn Width="115" Header="Column 5" DisplayMemberBinding="{Binding Member5}" />
</GridView>
</ListView.View>
</ListView>
是返回对象集合的Items
的属性)
要向DataContext
添加行,您只需要在ListView
集合中添加一个项目(请注意,该集合应该实现Items
,以便INotifyCollectionChanged
已通知; ListView
类适用于大多数情况)