Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two different styles for my window:

  1. Regular - window has title bar and can be moved/resized
  2. Fixed - window has no title bar and is fixed at the center of the screen

The window is too wide for either of the monitors on my development machine, but it's a perfect fit for the target/install machine. So, when debugging, I need to be able to move the Window so I can see everything on it, but when I release the app, I need it to run in "full screen" mode (like a PowerPoint app in projector mode).

Is there any way to set the Style property of the window based on whether I'm compiling in Debug vs. Release mode? I was thinking I might be able to use a binding, but I'm not quite sure how to implement it.

like image 280
devuxer Avatar asked Sep 03 '09 14:09

devuxer


1 Answers

Create a Style picker 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 + a instance of the StylePicker and set the ReleaseStyle and DebugStyle to the previous set up 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 markup set up the WindowStyle like this:

<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 the DebugReleaseStylePicker to set the style to any other control not just the Window.

like image 142
Claudiu Mihaila Avatar answered Oct 20 '22 07:10

Claudiu Mihaila