Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF show control in debug mode only

Tags:

I have some useful wpf buttons to test some functionality. It would be good not to show them in release but in debug indeed.

Doing it from code is easy. But I'd prefer a declarative solution.

like image 815
naeron84 Avatar asked Mar 12 '10 09:03

naeron84


2 Answers

The only solution I know of is to create a static property somewhere like this:

    public static Visibility IsDebug     { #if DEBUG         get { return Visibility.Visible; } #else         get { return Visibility.Collapsed; } #endif     } 

Then use it in XAML like this:

<MyControl Visibility="{x:Static local:MyType.IsDebug}" /> 

XAML doesn't have anything for compiler flags.

like image 187
Steven Avatar answered Oct 24 '22 15:10

Steven


This will show when the debugger is attached. First, set the namespace:

xmlns:diag="clr-namespace:System.Diagnostics;assembly=mscorlib" 

then set your resource:

    <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/> 

then use the binding:

<MenuItem Header="onlyIfDebuggerAttached" Visibility="{Binding Source={x:Static diag:Debugger.IsAttached}, Converter={StaticResource BoolToVisibilityConverter}}" /> 
like image 22
DanW Avatar answered Oct 24 '22 16:10

DanW