Search code examples
c#xamlvisual-studio-2015uwpvisual-studio-designer

Text visible on VS 2015 designer when control's text field value is binded


Whenever I bind text property to view model property

<TextBlock Text="{Binding SomeExampleText}"/>

on the designer I see nothing in the place where my text will appear in runtime. When I use x:Bind:

<TextBlock Text="{x:Bind ViewModel.SomeExampleText}"/>

on the designer I see "ViewModel.SomeExampleText", sometimes it does not display full length because of lacking space (if the binding path is too long).

Is there any way to have custom text displayed in designer just for preview instead of binding path or nothing as ashown above?


Solution

  • There are ways to create view models specifically for design time. The simplest approach is probably this:

    <TextBlock Text="{x:Bind ViewModel.SomeExampleText, FallbackValue='Hello!'}"/>
    

    That one shows the string "Hello" in the designer, both with Binding and x:Bind.

    For Binding you can set the design-time data context something like this:

    <Page
        ...
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:viewModels="using:MyNameSpace.ViewModels"
        d:DataContext="{d:DesignInstance Type=viewModels:DesignTimeViewModel, IsDesignTimeCreatable=True}"
        mc:Ignorable="d">
    

    The DesignTimeViewModel doesn't need any spcecific relation to your run-time view model; it only needs to have suitable properties with the same names. If you are binding to collections, this is probably your best bet.