How to see design-time data-binding in XAML editor (it works in runtime)?

Tar picture Tar · May 6, 2013 · Viewed 31.7k times · Source

I data-binded version number to appear as follows:

<Window <!-- ... --> DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <TextBlock>
            Version is: 
            <Run Text="{Binding Version, Mode=OneWay}"></Run>
            and advancing...
        </TextBlock>
    </Grid>
</Window>

and it's working during run-time.

How can I see it during design-time in the XAML editor in Visual Studio 2012 ? I only see:

Version is: and advancing...

instead of:

Version is: 5.2.2 and advancing...

EDIT - My solution:

Jure's answer below works, but I ended up using a dummy view-model static code technique, which works better for me since the data is a mock of the real view-model type:

d:DataContext="{Binding Source={StaticResource DesignViewModel}}" ...

Answer

XAMeLi picture XAMeLi · May 6, 2013

Make sure that you have these definitions at the root tag of your xaml file (in your case the Window tag):

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

Then, anywhere in the xaml (including the root tag) you can add this:

d:DataContext="{d:DesignInstance myNamespace:MyViewModel, IsDesignTimeCreatable=True}"

Now you just need to make sure that you initialize the values in a constructor or have default values for properties.

If you need to run a special logic for design mode, look at this answer.