Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
im writing LinearGradientBrush code in window.resource like below
XML
<Window.Resources>
 <LinearGradientBrush x:Key="cbColor">
           <GradientStop Offset="0" Color="Red"></GradientStop>
           <GradientStop Offset="0.5" Color="Silver"></GradientStop>
           <GradientStop Offset="1" Color="Green"></GradientStop>

 </LinearGradientBrush>

   </Window.Resources>

and using this as a background of window as a static resource but it is giving some error.
and if am using it as a dynamic resource it working fine,im confused why it is not working with static resource as i am writing it only in window xaml code

What I have tried:

i tried to write in window.resource with static resource tag
Posted
Updated 18-Feb-16 6:46am
v2

1 solution

There's a good explanation of the problem on StackOverflow:
c# - Windows Style from ResourceDictionary don't apply - Stack Overflow[^]

Basically, the Window is created before the nested ResourceDictionary is processed. At that point, nothing defined in the <Window.Resources> dictionary is available.

If you use a StaticResource, the designer will fail and the application will crash.

If you use a DynamicResource, the window will initially be created without the resource being applied; once the nested ResourceDictionary has been processed, and its resources are available, the window style will be updated.

A better solution is to move your resources to the <Application.Resources> dictionary, which is processed before the window is created.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900