Does XAML have a conditional compiler directive for debug mode?

I recently had to do this and was suprised at how simple it was when I couldn’t easily find any clear examples. What I did was add the following to AssemblyInfo.cs:

[assembly: XmlnsDefinition( "debug-mode", "Namespace" )]

Then, use the markup-compatability namespace’s AlternateContent tag to choose your content based on the presense of that namespace definition:

<Window x:Class="Namespace.Class"


        Width="400" Height="400">


            <mc:Choice Requires="d">
                <Style TargetType="{x:Type ToolTip}">
                    <Setter Property="FontFamily" Value="Arial"/>
                    <Setter Property="FlowDirection" Value="LeftToRight"/>
                <Style TargetType="{x:Type ToolTip}">
                    <Setter Property="FontFamily" Value="Tahoma"/>
                    <Setter Property="FlowDirection" Value="RightToLeft"/>


Now, when DEBUG is defined, “debug-mode” will also be defined, and the “d” namespace will be present. This makes the AlternateContent tag choose the first block of code. If DEBUG is not defined, the Fallback block of code will be used.

This sample code was not tested, but it’s basically the same thing that I’m using in my current project to conditionally show some debug buttons.

I did see a blog post with some example code that relied on the “Ignorable” tag, but that seemed a lot less clear and easy to use as this method.

Leave a Comment