visual studio – Reloade DesignViewModel after change


I try to update the Visual Studio XAML Design Preview when changing a value in the DesignViewModel

For example the simple view MainWindow.xaml with only one Text box

<Window x:Class="App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:App"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        d:DataContext="{d:DesignInstance local:DesignVM, IsDesignTimeCreatable=True}">
    <Grid>
        <TextBlock Text ="{Binding Name}"/>
    </Grid>
</Window>

The line d:DataContext="{d:DesignInstance local:DesignVM, IsDesignTimeCreatable=True}"> conects to the design time view model DesignVM in the name space defined as local by xmlns:local="clr-namespace:App"
This also allows auto completion and navigation for the DesignVM.

The DesignVM.cs looks like this:

namespace App
{
    class DesignVM
    {
        public string Name { get; set; } = "Test String";
    }
}

This is basically working but only after ruining the App.
So if I change the DesignVM.Name I need to run the App before I can see a Change in the XAML Design Preview.

This is OK if you only need an example text but if your view has different configurations because of values, e.g. it would be helpful if I could flip a bool like IsMapVisible to change the Design Preview with out starting the Application in between.

For now I have a Workaround by inheriting from DesignVM for specific configurations.
but this do not feel right.

Leave a Reply

Your email address will not be published. Required fields are marked *