user1283776
user1283776

Reputation: 21764

WPF window has different colors in VS designer than when debugging?

The WPF window background color is white during design in Visual Studio, but when I debug the application, it is black instead. Why?

Here is my .xaml code:

<Controls:MetroWindow x:Class="XLTT.Views.About"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

         ShowTitleBar="False"
         WindowStartupLocation="CenterOwner" 
         ShowCloseButton="False"
         ResizeMode="NoResize" 
         WindowStyle="ToolWindow"
         Height="320" 
         Width="400" 
         Title="About" >


<!-- your content here -->
<Grid Margin="20">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Name="lblAppName" Margin="10" TextWrapping="Wrap" Text="Application Name :" VerticalAlignment="Stretch" FontSize="24" FontFamily="Segoe UI Light" Foreground="#FFF53800"/>
    <TextBlock Name="lblBuild" Margin="10" TextWrapping="Wrap" Text="Build :" VerticalAlignment="Top" Grid.Row="1" FontSize="16" FontFamily="Segoe UI Light"/>
    <TextBlock Name="lblOwner" Margin="10" TextWrapping="Wrap" Text="Owner :" VerticalAlignment="Top" Grid.Row="2" FontWeight="Bold" FontFamily="Segoe UI Light" FontSize="16" />
    <TextBlock Name="lblLicense" Margin="10" TextWrapping="Wrap" Text="License" VerticalAlignment="Top" Grid.Row="3" FontWeight="Bold" Foreground="#FFEA1818" FontFamily="Segoe UI Light" FontSize="16" />
    <Button Name="btnOk" Content="OK" HorizontalAlignment="Right" Margin="0,0,10,4" VerticalAlignment="Bottom" Width="94" Grid.Row="4" Click="btnOk_Click" Height="40"/>

</Grid>

Colors in designer Colors when running

Upvotes: 1

Views: 1642

Answers (1)

Look at your App.xaml File.

If it contains:

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />

Replace it with:

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

Explanation

Your App.xaml is the entry point for your program an will apply the style settings to every child Window / Page / Control, but often the VS Designer can't load all resources in designing mode. So in VS it will be default white and on running the Application applies the styles completely.

MahApps use 2 default style templates for windows:

"BaseLight", "BaseDark"


Testing

Of course if it's not in the App.xaml you can check this with ThemeManager.DetectAppStyle(this); in your MetroWindow Constructor debugging.

Or override in your App.xaml.cs the OnStartup() Methode like this:

public partial class App : Application
{
    protected override void OnStartup (StartupEventArgs e)
    {
        // get the theme from the current application
        var theme = ThemeManager.DetectAppStyle(Application.Current);

        // now set the Green accent and dark theme
        ThemeManager.ChangeAppStyle(Application.Current,
                                    ThemeManager.GetAccent("Blue"),
                                    ThemeManager.GetAppTheme("BaseLight"));

        base.OnStartup(e);
    }
}

UPDATE

After your telling us that there is no App.xaml Entry Point in your Application the problem is clear. MahApps requires some resource dictionaries, as you can read at the Quick Start Guide.

I think in your case you just missing the styles for MahApps. So add, after your <Controls:MetroWindow> Tag following:

<Controls:MetroWindow.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Controls:MetroWindow.Resources>

Upvotes: 2

Related Questions