0

I am trying to create my own data template and bind data to that and use that template in Mainpage.xaml, The template was created in DataTemplates.xaml, and linked or loaded in App.xaml.

I followed this resource: http://igrali.com/2015/06/14/how-to-use-compiled-bindings-xbind-from-a-resource-dictionary/ Someone please help me in displaying the data in UI. I am doing this in windows 10 Universal App Development.

Thanks in Advance.

    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>

My DataTemplates.xaml code:

    <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>

My MainPage.xaml code:

    <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>
Sinatr
  • 20,892
  • 15
  • 90
  • 319
djkpA
  • 1,224
  • 2
  • 27
  • 57
  • What is not working? Exception? Could be this [bug](http://stackoverflow.com/q/5057213/1997232), try to define `DataTemplate` right inside `ItemTemplate` (like it is done [here](http://www.wpf-tutorial.com/listview-control/listview-data-binding-item-template/)). – Sinatr Jul 01 '15 at 08:40
  • I did the same way as in bug.And Unable to define more that one textBlock in DataTemplate right inside itemTemplate – djkpA Jul 01 '15 at 08:50
  • I will emphasize **what is not working**? You can have only 1 control inside `DataTemplate` (or `ItemTemplate`), but it can be a *container* (e.g. `StackPanel`) to hold many controls inside. – Sinatr Jul 01 '15 at 08:52
  • Thanks alot it worked – djkpA Jul 01 '15 at 09:04

1 Answers1

1

Need to bind ItemSource proeprty of ListView. You may use binding in DataTemplate to display dynamic data.

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

Sham
  • 910
  • 8
  • 15
  • But in windows 10 they are suggesting to use x:Bind as it loads very fast compared to Binding and checks error in build time itself. Thanks :) – djkpA Jul 01 '15 at 09:05
  • Ultimately ItemSource is the thing which need to set. – Sham Jul 01 '15 at 10:18