Ignoring Elements and Attributes

Sometimes you want to insert some XAML into a view, but have it ignored at runtime. You can do so with the Ignorable attached property, from the markup compatibility namespace. You use this property to specify which namespace prefixes the XAML processor should ignore. You might have noticed that this is defined in all new XAML files, and is used to instruct the XAML processor to ignore the design-time properties from the Expression Blend namespace. Say you have the following XAML, which simply contains a Label control:

<UserControl x:Class="AdventureWorks.TestView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

   <sdk:Label Content="A Label control from the sdk: namespace" />
</UserControl>

In this XAML, note how it has the mc namespace prefix defined. The Ignorable property from this namespace is attached to the UserControl element, and its value is set to d. This means that any element or attribute that uses the d prefix in the XAML will be ignored by the XAML processor at runtime. For example, the DesignHeight and DesignWidth attached properties used in the code, all using the d namespace prefix, are consequently ignored at runtime.

You can ignore additional namespaces by appending them to the Ignorable property's value, separated by a space. For example, if you wanted to also ignore elements using the sdk namespace at runtime, resulting in the Label control not appearing, you can set the Ignorable attribute's value like so:

mc:Ignorable="d sdk"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset