I've got some code written in C# WPF, and I've got some code for debugging, which I currently compile on or off for debug or release mode. How can I enable or disable UI controls which are currently written in my XAML based on C# preprocessor definitions?
-
There is no preprocessor in C# but there are preprocessor directives that are processed as if there was one. http://msdn.microsoft.com/en-us/library/ed8yd1ha(v=VS.100).aspx – Jeff Yates Nov 28 '10 at 17:44
-
possible duplicate of [XAML Conditional Compilation](http://stackoverflow.com/questions/1213576/xaml-conditional-compilation) – Robert MacLean Jul 02 '13 at 11:03
-
Yes. see https://stackoverflow.com/a/19940157/492 – CAD bloke Mar 04 '20 at 00:55
3 Answers
You can add some code in the constructor that enables/disables the elements:
public MainWindow()
{
InitializeComponent();
#if DEBUG
button1.IsEnabled = false;
#endif
}

- 29,160
- 8
- 71
- 111
-
I was really thinking of how they could just not exist in the first place, but I guess that this will do. – Puppy Nov 28 '10 at 12:27
-
It isn't possible to parse XAML based on preprocessor symbols. I believe that is what you're looking for? – Pieter van Ginkel Nov 28 '10 at 12:30
-
1You can't do it on preprocessor symbols, but Xaml does have support for conditionally compiling sections out, using the Markup Compatibility features. ptahmose's answer links to a question that links to an article on those. It's as close as you'll get. – Ian Griffiths Nov 28 '10 at 14:50
-
-
Not really helpful -- I need the namespace of a component to change based on preprocessor directive... can't change an xmlns attribute at runtime! – BrainSlugs83 Jul 02 '14 at 21:03
-
Another common use case might be if I have a user control set to internal versus public -- you can do this easily on the C# side with a preprocessor directive -- in XAML you have to specify the x:ClassModifier="internal" attribute -- this comes into effect at code generation / compile time -- how do I make this work with a preprocessor directive in XAML? – BrainSlugs83 Jul 02 '14 at 21:08
-
I don't see ptahmose's answer that Ian is referring to, but [bj0's answer](http://stackoverflow.com/a/19940157/1890705) to [Does XAML have a conditional compiler directive for debug mode?](http://stackoverflow.com/q/8732307/1890705) covers this. – reirab Nov 25 '15 at 17:02
There are no preprocessor-style directives for XAML. However, you could include and exclude XAML files based on the build configuration, providing you with some control. This could provide you with a way of including variations of a file depending on the chosen build configuration. Of course, the downside is that you would have to maintain multiple versions of a file. This could be mitigated through the use of T4 templates so that the different files are auto-generated according to the selected configuration.

- 61,417
- 20
- 137
- 189
There are two ways to do this. One is using the Preprocessor directives that can mask complete sections of code running it only in a particular build. Or you can use the the Conditional Attribute to easily block out a complete method. http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=420
Here's a description of the difference between the two : http://www.thinkfarahead.com/2007/09/if-debug-vs-conditional.html . You can reference the controls in your code by providing an x:Name attribute in xaml and putting the code to disable the controls in conditional section of your code.
Updated: to be clearer mentioned x:Name attribute.

- 261,912
- 58
- 460
- 511
-
2Those links are about C# conditional compilation. They're not applicable to Xaml. The question was about Xaml. – Ian Griffiths Nov 28 '10 at 14:50
-
1No read carefully : "Controls defined in xaml" ... "based on C# prepocessor definitions" The controls are in xaml. He wants to disable in C# – basarat Nov 28 '10 at 14:54