我有一个清单。当在数据库上运行查询时,它用CustomObjects填充(匹配的结果被放入这个列表)
然而,我想要在列表视图中显示这些生成的自定义对象的每个名称属性,这是给我带来麻烦的部分。
我不确定如何设置列表视图的模板来显示列表中的数据。我所能得到的最远结果如下:如果查询结果有3个匹配,我可以使3个文本框"HELLO“出现在列表视图中。这是通过在列表视图的模板部分中放置一个说"HELLO“的文本框来实现的(否则在VS2012的网格模板中布局)。解决方案是否与绑定有关?
任何帮助都将不胜感激。(使用metro应用程序、windows 8、.NET 4.5)
我目前所拥有的内容类似于
List<CustomObject> CustObjList =... //gets matches from database
listView.ItemsSource = CustObjList;
那么在XAML中是这样的
<ListView.ItemTemplate>
<DataTemplate>
<UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
<ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">
<TextBlock x:Name="listViewText" Text="HELLO" <--(???) Margin="30,0,0,0" Foreground="Black"/>
发布于 2012-08-31 20:39:18
是的,它是你需要的绑定。
查看有关Windows8XAML应用程序中各种列表的MSDN article。
特别是,看看ItemTemplate
。注意TextBlock
是如何绑定到绑定集合中每个对象的属性的:<TextBlock Text="{Binding Description}"
<ListView x:Name="itemListView"
Margin="120,0,0,60"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
SelectionChanged="ItemListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="110" Margin="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
<Image Source="{Binding Image}" Stretch="UniformToFill"/>
</Border>
<StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Description}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
发布于 2012-08-31 20:37:51
听起来像是要将数据放到ListView中,只是没有正确绑定属性。在textbox所在的模板中,将TextBox的Text属性更改为Text={Binding YourPropertyName"}。这是一篇很棒的文章,可以帮助您了解基础知识... http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh780650(v=win.10).aspx
https://stackoverflow.com/questions/12214210
复制相似问题