wpf - Shared definition in ResourceDictionary or CodeBehind? -


just wondering ... shared definitions (e.g. colors, brushes), better put in resourcedictionary (in xaml) or code-behind (c#)? assumption: usage of these definitions in both xaml , code-behind.

for example colors definition, seems every tutorial read off internet put in resourcedictionary - <solidcolorbrush x:key="color1" color="#cccccc"/>.

however, won't better if put in code-behind public static solidcolorbrush color1 = new solidcolorbrush(#cccccc); , accessing in xaml via x:static extension - {x:static local:mycolor.color1}?

in way, can nice intellisense in both xaml , code-behind. in addition, avoid hard coding of key in code behind - solidcolorbrush color1 = findresource("color1") style.

edit: objective to:

  • have nice intellisense in both xaml , codebehind
  • unified definition ensure no chance of runtime error due hard-coding
  • better performance

so when designer access background="{staticresource color1}" in xaml code, same thing textblock.background = color1; in codebehind. , when change x:key="color1", @ least there compile error.

personally i'd put on resources side, because can use blend visually see happening when change designer. great if application takes long time load up.

check out accessing resources code behind still have in 1 place. accessing resource via codebehind in wpf

edit: if using resource dictionary display in blend/design mode without having start application. if application takes while load up, saves lot of time modifying color, starting, checking, stopping application. can't speak it's performance implications, in general if can in xaml , not code behind it's best there.


Popular posts from this blog