Why is WPF design-time data out-of-date?

97 views Asked by At

I am using Visual Studio 2022 (17.9.3) and have created a .NET Framework 4.8 library project containing controls with MVVM databinding to viewmodels following a pattern like this:

<UserControl x:Class="WpfControls.TypeListControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MyControlLibrary"
         xmlns:viewModels="clr-namespace:MyControlLibrary.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="250" d:DesignWidth="200">
    <UserControl.Resources>
        <viewModels:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>
    <Grid d:DataContext="{Binding Source={StaticResource MyViewModel}}">
        <!-- etc.. -->
    </Grid>
</UserControl>

The controls and associated viewmodels are all contained in the same project (assembly).

I am seeing design-time data displayed in the control, except that as I build up the controls and build out the viewmodels, I am seeing the controls display out-dated content from the viewmodels. It is as if the designer is caching the assembly and not invalidating it as I make changes and rebuild.

One impact of this is that the designer isn't seeing newly added types (such as viewmodels), so XAML references are triggering errors.

I have tried reloading the project, reloading the solution, restarting Visual Studio, deleting the .obj and .bin folders, deleting the .vs file, all without any impact. This leads me to believe that the VS designer is caching the assembly somewhere, but I don't know where. If this is the case, where is that cache located, and how can I clear it?

EDIT:

My solution includes a WPF application which consumes the library. If I rebuild the application, the library design views update to show correctly. It looks as if there is a cache which is correctly invalidated by an application build, but not by a build of the component assembly.

EDIT#2:

I have revised the data context per Hui Liu to the following:

<UserControl ...
    d:DataContext="{d:DesignInstance viewModels:MyViewModel, IsDesignTimeCreatable=True}"
    ...>

which I understand to be better/preferred. However, it doesn't alter the behavior at the root of my question.

Library project:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Library</OutputType>
    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
    <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
  </PropertyGroup>
</Project>

Application project:

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
    <PropertyGroup>
        <TargetFramework>net48</TargetFramework>
        <OutputType>WinExe</OutputType>
        <UseWPF>True</UseWPF>
        <StartupObject />
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <DebugType>full</DebugType>
        <DebugSymbols>true</DebugSymbols>
    </PropertyGroup>
    <ItemGroup>         
        <ProjectReference Include="..\MyControlLibrary\MyControlLibrary.csproj" />
        <Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
        <EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
        <Compile Update="Properties\Resources.Designer.cs" AutoGen="True" DependentUpon="Resources.resx" DesignTime="True" />
        <None Update="Properties\Settings.settings" Generator="SettingsSingleFileGenerator" LastGenOutput="Settings.Designer.cs" />
        <Compile Update="Properties\Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
    </ItemGroup>
    <ItemGroup>
        <Reference Include="PresentationCore" />
        <Reference Include="PresentationFramework" />
        <Reference Include="System.Xaml" />
        <Reference Include="WindowsBase" />
    </ItemGroup>
</Project>
0

There are 0 answers