Just wondering ... for shared definitions (e.g. colors, brushes), is it better to put it in resourcedictionary (in XAML) or code-behind (C#)? Assumption: usage of these definitions are in both XAML and code-behind.
For example colors definition, it seems like every tutorial I read off the internet will put it in resourcedictionary - <SolidColorBrush x:Key="Color1" Color="#cccccc"/>
.
However, won't it be better if it is put in code-behind as public static SolidColorBrush Color1 = new SolidColorBrush(#cccccc);
and accessing it in XAML via x:Static extension - {x:Static local:MyColor.Color1}
?
In this way, I can get the nice intellisense in both XAML and code-behind. In addition, avoid the hard coding of the key in the code behind - SolidColorBrush Color1 = FindResource("Color1") as Style
.
Edit: My objective is to:
- have the nice intellisense in both xaml and codebehind
- unified the definition to ensure no chance of runtime error due to hard-coding
- better performance
So that when the designer access to Background="{StaticResource Color1}"
in the XAML code, it is the same thing as TextBlock.Background = Color1;
in the codebehind. And when someone were to change the x:Key="Color1"
, at least there will be a compile error.