0

I am learning WPF and found that the way it work is completely different. I have two questions about the following XAML markup:

<Button x:name="test" BackGround="{StaticResource MyColor}" />

1) Why x:name? Since name is a property, then why x should be prefix it. Is there any special meaning for x:name?

2) What is StaticResource and where is StaticResource stored?

Please explain in detail. Thanks.

Carlo
  • 25,602
  • 32
  • 128
  • 176
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

2

x is a prefix for an XML namespace. Here is the official doc on this: XAML Namespaces and Namespace Mapping for WPF XAML

If you want more on the XML namespace topic (unrelated to XAML): http://wap.w3schools.com/xml/xml_namespaces.asp

StaticResource is a "Markup extension", here is the official doc again: StaticResource Markup Extension

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
1

About x:Name you should read this thread.

Xaml parser transformed directive :

{StaticResource MyColor} 

into C# code like this:

Resource["MyColor"]

Static resource lookup behavior

  1. The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
  2. The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.
  3. Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.

Source

Simply if you define something in App.xaml, parent or current control Resources you can use StaticResource to lookup those to get the value under the key.

Community
  • 1
  • 1
bartosz.lipinski
  • 2,627
  • 2
  • 21
  • 34