a basic question about WPF
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 pr开发者_高级运维operty, 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.
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
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
- The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
- 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.
- 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.
精彩评论