I need something like this for styles in XAML :
<Application.Resources>
#if DEBUG
<Style TargetType="{x:Type ToolTip}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FlowDirection" Value="LeftToRight"/>
</Style>
#else
<Style TargetType="{x:Type ToolTip}">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FlowDirection" Value="RightToLeft"/>
</Style>
#endif
</Application.Resources>
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:
Then, use the markup-compatability namespace's AlternateContent tag to choose your content based on the presense of that namespace definition:
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.
You could use a template selector. The DataTemplateSelector class is something you code. With the template selection method that you override, you could put your preprocessor directives.
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx
This is not possible in WPF/Silverlight/WP7.
On an interesting note, the standards document, ISO/IEC 29500, covers how this should be handled in an XML document, and XAML does support one of the items from that spec
mc:Ignorable
which allows us to do things like this:to comment out attributes. I do think it would be cool if XAML one day supported the rest of the spec that allows the loading of alternate content.
The
mc:Ignorable
attribute is used by Blend to support design time functionality.I feel like the given answers aren't the easiest to use. Here is my solution using a custom attachable dependency property:
and the xaml would be used like this:
I kept it as a bool in case you wanted to add some other visibility logic in there. This is a nice simple toggle that can be bound to and attached to any control