Reputation: 3025
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.
Upvotes: 27
Views: 9255
Reputation: 3306
As of at latest Visual Studio 17.2.6 / WinUI3, the following simple beauty seems to work (details not related to visibility excluded):
<Page
xmlns:appmodel="using:Windows.ApplicationModel">
<Button Visibility="{x:Bind Path=appmodel:Package.Current.IsDevelopmentMode}"/>
</Page>
Upvotes: -1
Reputation: 2056
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}}" />
Upvotes: 23
Reputation: 11754
Based on Steven answer... You can use a static class and declare the visibility once only.
using System.Windows;
namespace HQ.Wpf.Util
{
/* Usage:
xmlns:wpfUtil="clr-namespace:HQ.Wpf.Util;assembly=WpfUtil"
<Button Name="CmdTest" Click="CmdTestOnClick" Visibility="{x:Static wpfUtil:DebugVisibility.DebugOnly}">Test</Button>
*/
public static class DebugVisibility
{
public static Visibility DebugOnly
{
#if DEBUG
get { return Visibility.Visible; }
#else
get { return Visibility.Collapsed; }
#endif
}
public static Visibility ReleaseOnly
{
#if DEBUG
get { return Visibility.Collapsed; }
#else
get { return Visibility.Visible; }
#endif
}
}
}
XAML:
<Button Name="CmdTest" Click="CmdTestOnClick"
Visibility="{x:Static wpfUtil:DebugVisibility.DebugOnly}">Test
</Button>
Upvotes: 1
Reputation: 141
Not sure what the difference is between this and Steven's approach, but I used his property as a non-static property in a non-static class, and referenced it like such:
<local:MyClass x:Key="MyClass" />
<MyControl Visibility="{Binding IsDebug, Source={StaticResource MyClass}, Mode=OneTime}" />
Upvotes: 0
Reputation: 4963
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.
Upvotes: 33
Reputation: 273219
As far as I know there is no way to use the Configuration constants (Debug, Release) from XAML.
So the best you can get is to bind the Visibility property of the buttons to a Debug property on your datacontext. But setting that property would still require some code.
Upvotes: 2