0

I have a List. It is populated with CustomObjects when a query is run on a database (and matching results are put into this List)

However, I then want to display each of the e.g. name properties of these resultant custom objects in a listview, this is the part that is causing me trouble.

Im unsure how to set up the template of the list view to display the data from the list. The furthest I have been able to get is as follows: If the result of the query has 3 matches, I can make 3 textboxes saying "HELLO" appear in the list view. This is achieved by putting a textbox saying "HELLO" in the template part of the listview (which is otherwise laid out as it is in the grid template of VS2012). Is the solution something to do with bindings?

Any help would be much appreciated. (working on a metro app, windows 8, .NET 4.5)

what I currently have looks something like this

List<CustomObject> CustObjList =... //gets matches from database
listView.ItemsSource = CustObjList;

then in the XAML is this

<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"/>
4

2 回答 2

0

听起来您正在将数据放入 ListView,只是没有正确绑定属性。在文本框所在的模板中,将 TextBox 上的 Text 属性更改为 Text={Binding YourPropertyName"}。这是一篇很棒的文章,可帮助您了解基础知识... http://msdn.microsoft.com/en -us/library/windows/apps/xaml/Hh780650(v=win.10).aspx

于 2012-08-31T12:37:50.993 回答
0

是的,您需要的是绑定。

查看这篇MSDN 文章,了解 Windows 8 XAML 应用程序中的各种列表。

特别是,查看ItemTemplate. 请注意TextBlocks 如何绑定到绑定集合中每个对象的属性:<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-31T12:39:18.470 回答