Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView DataTemplate in separate folder

I'm trying to learn programming apps for Universal Windows Platform. I'm currently working with ListView and I define its layout in <DataTemplate>, but the code is a one mess. Is there a way to define <DataTemplate> in a separate folder? I searched the net but I wasn't able to find a solution. Could you please help me with that? Thanks.

like image 268
miskohut Avatar asked Oct 28 '25 06:10

miskohut


1 Answers

I would always recommend creating a ResourceDictionary for this kind of thing. Here's an example setup:

Create a folder Resources > Add > New item > Resource Dictionary "Templates.xaml"

In your App.xaml add

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Templates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

In Templates.xaml you can add any template you want, like so:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:thestory.Resources">


<DataTemplate x:Key="ListItemTemplate">

</DataTemplate>

You can now reference this template wherever you need it using {StaticResource ListItemTemplate}

Good Luck!

PS: I would actually also recommend doing the same for styles and other application wide resources like font sizes, brushes, backgrounds etc.

like image 89
Ferdinand von den Eichen Avatar answered Oct 30 '25 03:10

Ferdinand von den Eichen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!