1

Say I have a simple ItemTemplate for a ListView

<ListView ItemsSource="{x:Bind ListItems}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="local:TextItem">
            <TextBlock Text="{x:Bind Item}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MainPage.xaml

When I run the app, I get a ListView populated with TextBlocks - one for each item in ListItems (which is a behind defined variable). However, in the XAML designer, I see nothing.

So, is there a way to preview the ItemTemplate/DataTemplate in the XAML designer, with a set number of placeholder TextBlocks with placeholder text replacing the Text="{x:Bind Item}"? Or just preview a single TextBlock?


This is not a duplicate of

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • If it's needed, I can add the behind code in a little while – binaryfunt Feb 27 '18 at 21:11
  • It seems you are looking for a solution for design time data in UWP... there should be several links available on MSDN, maybe check those out? Like https://learn.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer BUT there does seem to be a problem with FCU https://developercommunity.visualstudio.com/content/problem/172286/design-time-data-in-uwp-library-not-working-in-vs.html – Depechie Feb 27 '18 at 22:38
  • @Depechie I have read that docs page, but it says "if you're using `{x:Bind}` then your bindings at least show placeholder values on the design surface". Perhaps this is to do with the apparent bug that prevents use of Blend? If so, I can only wait until they patch it – binaryfunt Feb 27 '18 at 22:50
  • Can use Blend? It can see it. – lindexi Feb 28 '18 at 03:54
  • @lindexi No I can't use Blend, as all instructions for using it result in me seeing the message 'This feature is not available for projects targeting "Windows 10 Fall Creators Update (10.0; Build 16299)"', which might be because of a bug, as Depechie has suggested – binaryfunt Feb 28 '18 at 14:59
  • Have you implemented DataContext in your xaml? – Nico Zhu Mar 01 '18 at 08:24
  • @NicoZhu-MSFT I thought you can't use DataContext with x:Bind? (Btw, I was missing an x:DataType - now added) – binaryfunt Mar 01 '18 at 15:49
  • I used `Binding` in `DataTemplate`. The xaml designer worked. – Nico Zhu Mar 02 '18 at 01:46
  • I'm having this issue too. I want to use `x:Bind` since its compile time checked but I can't see any data in the designer and there seems to be no way to set it. – Chinwobble May 23 '20 at 14:14

1 Answers1

3

Microsoft actually posted a (semi helpful) tutorial on this in April of this year: link

Requirements

Whatever you want to display, you will need this at the top of your XAML code (should be there by default if you created the page using VS's templates):

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"

A simple list of strings

For something simple like a list of strings, something like this will work just fine (taken from the above mentioned MSDN page):

<ListView>
    <d:ListView.Items>
        <system:String>Item One</system:String>
        <system:String>Item Two</system:String>
        <system:String>Item Three</system:String>
    </d:ListView.Items>
</ListView>

Display something more complex

If you want to use a data source from somewhere else in your code, it gets a little more ugly:

First you need to set your data context (usually done at the page level, but you can also add it just on the element where you want to use it):

    d:DataContext="{d:DesignInstance
                  yournamespace:YourDataSourceClass,
                  IsDesignTimeCreatable=True}"

And on the ListView add:

d:ItemsSource="{Binding data}"

If you are using an ItemTemplate, you will need to add "d:" variants for everything you want to pull from your data source.

Full example code:

XAML:

<Page
    x:Class="exampleApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:exampleApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
        d:DataContext="{d:DesignInstance local:DesignTimeData,
                                     IsDesignTimeCreatable=True}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid>
            <ListView ItemsSource="{x:Bind ListItems}" d:ItemsSource="{Binding DummyData}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="local:TextItem">
                        <TextBlock Text="{x:Bind Item}" d:Text="{Binding Item}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Grid>
</Page>

C#:

using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;

namespace exampleApp {
    public sealed partial class MainPage : Page {
        public ObservableCollection<TextItem> ListItems;
        public MainPage() {
            this.InitializeComponent();
            //populate you ListItems list
        }
    }
    public class TextItem {
        public string Item { get; set; }
    }
    public class DesignTimeData {
        public ObservableCollection<TextItem> DummyData { get; set; } = new ObservableCollection<TextItem> {
        new TextItem { Item = "foo" },
        new TextItem { Item = "bar" },
        new TextItem { Item = "bla" },
        new TextItem { Item = "blubb" }
        };
    }
}

Remarks

  • It seems you have to use "Binding" rather than "x:Bind" for the design-time bindings. From what I understand, x:Bind works using code generated at compile time, which is not executed by the XAML designer
  • When you change your Binding declarations or data source code, you will have to recompile to see the changes in the designer. Other design changes will reflect in real time
  • Binding is more limited than x:Bind in some regards (notably you cannot bind to class methods using Binding, only x:Bind). Keep that in mind when writing your design-time data
  • Sadly some of the more convenient ways of getting design-time data into your app are WPF and Xamarin only (Blends' data window, data declared in XAML file, and especially auto-generated sample data)

(Everything was tested using Visual Studio 2019 version 16.10.3.)

karel
  • 5,489
  • 46
  • 45
  • 50