How can I use WPF one window style for Debug mode and another for Release mode?

I have two different styles for my window:

  • Normal - the window has a title bar and can be moved / resized.
  • Fixed - the window has no title and is fixed in the center of the screen.

The window is too large for any of the monitors on my development machine, but it is ideal for the target / installation machine. Thus, when debugging, I need to be able to move the Window so that I can see everything on it, but when I launch the application, I need it to work in full screen mode (for example, in PowerPoint application in projector mode).

Is there a way to set the Style property of a window based on whether I compile in debug or release mode? I thought I could use the binding, but I'm not quite sure how to implement it.

+6
c # visual-studio wpf xaml
source share
6 answers

Create a style selection class:

 namespace WpfApplication1 { public class DebugReleaseStylePicker { #if DEBUG internal static readonly bool debug = true; #else internal static readonly bool debug=false; #endif public Style ReleaseStyle { get; set; } public Style DebugStyle { get; set; } public Style CurrentStyle { get { return debug ? DebugStyle : ReleaseStyle; } } } } 

in your App.xaml add to your Application.Resources your debug and release style + instance of StylePicker and set ReleaseStyle and DebugStyle to the previous settings styles:

 <Application.Resources> <Style x:Key="WindowDebugStyle"> <Setter Property="Window.Background" Value="Red"></Setter> </Style> <Style x:Key="WindowReleaseStyle"> <Setter Property="Window.Background" Value="Blue"></Setter> </Style> <WpfApplication1:DebugReleaseStylePicker x:Key="stylePicker" ReleaseStyle="{StaticResource WindowReleaseStyle}" DebugStyle="{StaticResource WindowDebugStyle}"/> </Application.Resources> 

In your window layout, set WindowStyle as follows:

 <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" Style="{Binding Source={StaticResource stylePicker}, Path=CurrentStyle}"> .. </Window> 

You can reuse DebugReleaseStylePicker to set the style for any other control, not just the window.

+11
source share

It can be hard to do in XAML, but in real code you can just do something like:

 #if DEBUG window.Style = WindowStyles.Regular; #endif 

Why not put it somewhere that will be executed after the normal XAML code is executed?

+5
source share

You can create a markup extension like this:

 public class DebugStyleExtension : MarkupExtension { public object DebugResourceKey { get; set; } public object ReleaseResourceKey { get; set; } public object ProvideValue(IServiceProvider provider) { #if DEBUG return Application.Current.FindResource(DebugResourceKey) as Style; #else return Application.Current.FindResource(ReleaseResourceKey) as Style #endif } } 

you would use it like this:

 <Window ... xmlns:my="clr-namespace:MyNamespace" Style="{my:DebugStyle DebugResourceKey=DebugStyle, ReleaseResourceKey=NormalStyle}"> 
+3
source share

You can conditionally compile in the XAML file as well as in the codeb. read this article

basically, you do this in your \ AssemblyInfo.cs properties:

 #if BETA [assembly:XmlnsDefinition("BetaVersion", "Example.Technology")] #endif 

add xmlns to the * .XAML file:

 xmlns:beta="BetaVersion" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

Now you can do the following:

 <mc:Choice Requires="beta"> <Label> This is ALPHA software. Confidential. Internal use only. Do not distribute <Label> </mc:Choice> 

as a side note, this probably won't work with silverlight - AFIK assembly:XmlnsDefinition not supported

+2
source share

Could you use #if DEBUG to set the property to a different value and bind to it?

Can

 #if DEBUG style = 0; #else style = 1; #endif 

(Keep in mind that there is no VS.) and use an evaluation converter.

0
source share

Many useful answers ... I thought of another idea that I thought I would choose there: a value converter plus a binding:

Here's the value converter:

  public class WindowToWindowStyle : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var window = (Window)value; var style = (Style)window.FindResource("Window_FixedStyle"); #if DEBUG style = (Style)window.FindResource("Window_Style"); #endif return style; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; } } 

Here is my window declaration:

 <Window ... xmlns:local="clr-namespace:MyProj" Style="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ValueConverter_WindowToWindowStyle}}" WindowStartupLocation="CenterScreen"> <Window.Resources> <local:WindowToWindowStyle x:Key="ValueConverter_WindowToWindowStyle" /> </Window.Resources> ... </Window> 

What does it do:

So what happens here, I pass the link to the window itself to the value converter and return the appropriate style.

0
source share

All Articles