Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamicResource not working, but StaticResource does?

Here is the scenario:

1) Open Visual Studio 2008, create new project...

2) Select WPF Custom Control Library as the project type

3) Open the Generic.xaml resource dictionary located in the Themes folder

4) Add a simple brush such as:

< SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" / >

5) Change the Background property of the nested Borer Control from

Background="{TemplateBinding Background}"

to

Background="{DynamicResource BackgroundBrush}"

Now when you compile and use this control in any project, the background will NOT be yellow. BUT, if you change the background property to be a StaticResource as such

Background="{StaticResource BackgroundBrush}"

...it will work. Why is this? Why does StaticResource work but not DynamicResource???

like image 241
Max Avatar asked Nov 14 '22 14:11

Max


1 Answers

If you are hard coding the brush in Xaml file, that sounds pretty static to me.

The MSDN entry for DynamicResource say this is for when you want the use resolve the name of the resource at run time (ie the name is stored in a another resource), where you know the name at compile time.

Sounds very much like a pointer verse a pointer-to-a-pointer from C.

like image 167
Simeon Pilgrim Avatar answered Dec 23 '22 09:12

Simeon Pilgrim