Search code examples
c#wpfxamlresourceswpf-style

WPF window properties only visible at runtime


I'm a XAML-beginner and also learning it by myself. So in the App.xaml I have the following Styles:

<Application.Resources>
        <Style x:Key="Colors" TargetType="{x:Type Control}">
            <Setter Property="Background" Value="#FF404040"/>
            <Setter Property="Foreground" Value="#FF25CBDA"/>
            <Setter Property="BorderBrush" Value="#FF25CBDA"/>
        </Style>

        <Style x:Key="BaseWindowStyle" TargetType="Window" BasedOn="{StaticResource Colors}">
            <Setter Property="Title" Value="MainWindow"/>
            <Setter Property="WindowState" Value="Normal"/>
            <Setter Property="Icon" Value="Icon.ico"/>
        </Style>

        <Style x:Key="MainWindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource BaseWindowStyle}">
            <Setter Property="ResizeMode" Value="CanResize"/>
            <Setter Property="ShowInTaskbar" Value="True"/>
        </Style>
</Application.Resources>

That's how I use the Style in the MainWindow.xaml:

Style="{DynamicResource MainWindowStyle}"

My problem is that the properties, like the background, are only visible at runtime. It's probably obvious but I really don't get it.


Solution

  • Welcome, DeJoon! It's unclear exactly what it is that you don't get. What do you want to happen, or expect to happen? Are you saying that you don't know what the background color looks like until you run the app, but you want to see it at design time? You should be able to see the color and the properties via the XAML Designer.

    I took your code, commented out one line related to "icon.ico" because I don't have that file, and I was able to see your background color as a dark grey color from the designer. See here:

    enter image description here

    Here is my MainWindow.xaml.cs:

    <Window x:Class="StyleTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:StyleTest"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800" Style="{DynamicResource MainWindowStyle}">
        <Grid>
    
        </Grid>
    </Window>