数据模板绑定不在UI中显示数据

时间:2015-07-01 08:09:21

标签: c# wpf xaml data-binding

我正在尝试创建自己的数据模板并将数据绑定到该模板并在Mainpage.xaml中使用该模板,模板在DataTemplates.xaml中创建,并在App.xaml中链接或加载。

我关注了这个资源:http://igrali.com/2015/06/14/how-to-use-compiled-bindings-xbind-from-a-resource-dictionary/ 有人请帮我在UI中显示数据。 我在Windows 10 Universal App Development中这样做。

先谢谢。

    My App.xaml code:
    <Application
        x:Class="Listview.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Listview"
        RequestedTheme="Light">

        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <!--<local:DataTemplates/>-->
                    <ResourceDictionary Source="DataTemplates.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

我的DataTemplates.xaml代码:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Listview"
        x:Class="Listview.DataTemplates">
        <DataTemplate x:Key="IconTextDataTemplate">  
                <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                    <TextBlock Text="Ay Lorem Ipsum" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" />
                    <TextBlock Text="Dolor sit amet" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
                </StackPanel>

        </DataTemplate>
    </ResourceDictionary>

我的MainPage.xaml代码:

    <Page
        x:Class="Listview.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Listview"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

            <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                <ListView x:Name="IconTextGrid" Height="500" Width="500"
                  ItemTemplate="{StaticResource IconTextDataTemplate}" >
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid MaximumRowsOrColumns="8"/>
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                </ListView>

            </Grid>
    </Page>

1 个答案:

答案 0 :(得分:1)

需要绑定ListView的ItemSource proeprty。 您可以在DataTemplate中使用绑定来显示动态数据。

    <DataTemplate x:Key="IconTextDataTemplate">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <TextBlock Text="{Binding firstname}" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" />