Saturday, February 25, 2012

XAML Namespaces and Default Properties

Here are a few questions that have come up during some recent "XAML: Don't Fear the Markup" presentations.

Question: How can you tell what is in the namespaces that are included by default in the XAML markup?

MSDN and the Visual Studio Help contain listings of the elements that are in the namespaces.  For example, the "x:" namespace (xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml") is available in this article: XAML Namespace (x:) Language Features.  It's interesting to take a quick tour through this list.  It shows the "x:Class", "x:Name", and "x:Key" that were mentioned in the presentation; but it also shows things like "x:Null" which are useful for resetting a property that would normally be inherited.  For example, if you have a custom Style, you can choose to have that style automatically applied to all of the applicable controls in the XAML document.  If a control needs to use the default style rather than the custom style, you can set that control's style to "{x:Null}" to reset the value.

The other namespace that we looked at a little was the "d:" namespace (xmlns:d="http://schemas.microsoft.com/expression/blend/2008").  This also has an article describing the members: Design-Time Attributes in the Silverlight Designer.  This shows the "d:DesignHeight" and "d:DesignWidth" that we saw in the sample.  It also has some interesting things to look into further such as "d:DataContext" which lets you set up a design-time data context for data binding.  In our sample, the databinding was hooked up to a local object, so the design-time view worked without doing anything special.  But if you had remote data (for example, customer data coming from a SQL database), then you wouldn't have that data available at design time.  In that case, you could use the "d:DataContext" to hook up to a local object that had some sample hard-coded data.  These are all design-time functions.  They are there to help you with your XAML page/window/user control creation.  At run time, your actual data context would be used.

Question: The XAML namespaces use the URI format ("http://...").  How should you name your own namespaces?

When creating your own namespaces, you should follow whatever namespace naming convention that you have in place.  There is no need to use the URI format; instead, simply use the standard .NET namespace naming -- usually something like "CompanyName.Product.Module".  In the sample, we saw how to bring in a namespace that is local to our project, which ended up looking like this: xmlns:local="clr-namespace:JeremyBytes.StopWatch".  But it is fairly simple to bring in namespaces from other assemblies that are referenced in the current project.  You can get more information here: XAML Namespaces and Namespace Mapping for WPF XAML.

Question: How can you identify the default property of a XAML element?

The default property of a XAML element is the property that we can simply put "between the tags" without any specific references.  In our sample, we saw that we could specify the content property for a button by simply putting the content between the opening and closing tags: "<Button...>Start</Button>".  The question is how do we know which property is the default property?

The default properties are specified in the help file.  So, all we need to do is press "F1" on any of our controls to see what the value is.  Here are a few samples:



Here, we can see what the default properties are for each of these elements.  As we saw in the sample, we are always free to explicitly specify the property, but using the default property syntax can help keep our markup more readable.

If you have any questions about the XAML presentation or walkthrough, feel free to send them along to me.

Happy Coding!

No comments:

Post a Comment