Conditionals in XAML

Published on 03 december 2017

Ever wanted to display things conditionally in XAML based on a pre-processor directive like DEBUG?

Start by adding the following to your AssemblyInfo.cs

[assembly: XmlnsDefinition("", "MyProjectNamespace")]

Now, in your XAML file, you should be able to add a new namespace pointing to this definition. We also need to import the XAML compatibility namespace. To suppress the errors given by the XAML processor, use the mc:Ignorable attribute.

You should now be able to use AlternateContent, Choice and (optionally) Fallback elements in your XAML code to show different elements depending on your build configuration.

<Window x:Class="MyProjectNamespace.MyView"

        <mc:Choice Requires="debug">
            <TextBlock Text="Debugging!" />
            <TextBlock Text="Not debugging!" />


Be aware that content within AlternateContent will not show in the designer. If this is something you need, I recommend that you use a different technique.

Update (2017-12-05)

The AlternateContent element doesn't seem to be supported in UWP applications.