Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a WPF resource dictionary at design-time

Tags:

.net

wpf

I have a WPF window in which some properties are defined as dynamic resources like this:

<Window x:Class="LocSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        MinHeight="350" MinWidth="525"
        Title="{DynamicResource ResourceKey=ResId_Title}"
        FlowDirection="{DynamicResource ResId_FlowDirection_Default}" >
    <Grid>
       <Label Content="{DynamicResource ResId_FirstName}" />  
    </Grid>
</Window>

The ResourceDictionary is loaded at runtime to reflect the language choice of the user. And to enable the user to switch the language on the fly.

This works fine at runtime, but at design-time, the resource-defined properties are not shown. It is clear to me that the designer can't show them, because they are not defined at design-time.

I need a way to load a default ResourceDictionary at design-time so that the designer can show anything.

like image 659
Ulrich Proeller Avatar asked Jul 26 '12 12:07

Ulrich Proeller


1 Answers

EDIT:

In Blend, you can use a Design-time Resources Dictionary file for this case. Visual Studio can read that file but doesn't have the tooling to create one. But if you're not afraid of project files, you can do that by hand.

  1. Add a file DesignTimeResources.xaml to your project.
  2. Open your project file (*.csproj) and find registration of the DesignTimeResources.xaml file.
  3. Replace this registration with the following code
<Page Include="Properties\DesignTimeResources.xaml" Condition="'$(DesignTime)'=='true' OR ('$(SolutionPath)'!='' AND Exists('$(SolutionPath)') AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true')">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
  <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>

Now you can merge all the resource dictionaries you need in the file. Compiler will ignore it, but the designer won't.

See not my article for more details.

like image 189
vit Avatar answered Oct 16 '22 14:10

vit