John
John

Reputation: 1764

DynamicResource Cannot be resolved

I have been trying to learn about resources and styles, I want to create a chromeless window.

I have an example that acheives what I want via the following simple extracts of xaml.

I have a Resource set in Themes/Generic.xaml

<Style x:Key="BorderlessWindowStyle" TargetType="{x:Type Window}">
    <Setter Property="AllowsTransparency" Value="true" />
    <Setter Property="WindowStyle" Value="None" />
    <Setter Property="ResizeMode" Value="CanResizeWithGrip" />
    <Setter Property="Background" Value="Transparent" />
</Style>

I have a main window:

<Window x:Class="Project1.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Project1"
    x:Name="Window"
    Title="Shell" Height="576" Width="1024" Style="{DynamicResource BorderlessWindowStyle}">
<Grid></Grid>

But the style is not being applied and VS designer states it cannot resolve the resource.

The example I have been looking at does things this way and I cannot discover the difference between what I have seen done and what I am trying to do.

I thought that Genric.xaml was a 'special' resource dictionary that should be discoverable by my Window control - and I am guessing this assumption is my error.

What do I need to do to make this work? (Now I understand I can set these properties in the Window xaml directly, and I have done so and get the effect I want. BUT I really want to undersatnd using the Generic.xaml resource dictionary way as I have presented here)

best regards

John.

Upvotes: 1

Views: 9734

Answers (2)

Joseph Mawer
Joseph Mawer

Reputation: 298

Thought I would add the following example in case it helps some others out. To add a resource dictionary to your app.xaml file you can add the following xaml code to the app.xaml file.

<Application x:Class="ProjectX.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:ProjectX"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Use the Black skin by default -->
            <ResourceDictionary Source="Resources\ResourceFile.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Where 'Resources' coudl be a folder in your project that contains the Resource Dictionary file (ResourceFile.xaml).

The you can add code to your resource dictionary like such:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ProjectX.Resources">

<!-- The Background Brush is used as the background for the Main Window -->
<SolidColorBrush x:Key="MainBackgroundBrush" Color="#FF202020" />

</ResourceDictionary>

And then finally, dynamically bind to your resource dictionary doing something like:

<Window
  x:Class="ProjectX.MainWindow"
  Title="Family.Show" Height="728" Width="960"
  Background="{DynamicResource MainBackgroundBrush}"
  ResizeMode="CanResizeWithGrip">
</Window>

Upvotes: 0

EvAlex
EvAlex

Reputation: 2938

Themes/generic.xaml file is automatically used to find default styles for Custom Controls. In your case you have an ordinary Window with custom style. You cannot define this style in Window.Resources section, because the style should be defined at a higher level. The only higher level of Window is App.xaml, because the Window is in fact its child. That's why the solution for your question is to place the style into App.Resources section.

Upvotes: 7

Related Questions