1

I have a window with a grid containing an image and some buttons:

<Window x:Class="Wormholes.Views.TitleWindow"
        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:Wormholes"
            xmlns:commands="clr-namespace:Wormholes.Commands"
        mc:Ignorable="d"
        Title="Warning: Weird Wormholes!" Height="450" Width="800" WindowState="Maximized" WindowStyle="None">xi
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="64"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Source="/Images/Splash.png" Grid.RowSpan="2" Grid.ColumnSpan="3"/>
        <Button Grid.Row="1" Grid.Column="0" Command="commands:Commands.StartCommand">Start</Button>
        <Button Grid.Row="1" Grid.Column="2" Command="commands:Commands.ExitCommand">Exit</Button>
    </Grid>
</Window>

And a style in App.xaml:

<Application x:Class="Wormholes.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Wormholes"
             StartupUri="Views/TitleWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type Grid}">
            <Setter Property="Background" Value="Black"/>
        </Style>
    </Application.Resources>
</Application>

However when this style exists in App.xaml, then when I run my app, the image and buttons disappear as soon as the window opens! If I delete the style, the controls appear (unstyled of course). and if I put the style in Window.Resources, it works, but of course then it wouldn't apply to any other views I created. How can I make the style work from App.xaml?

ekolis
  • 6,270
  • 12
  • 50
  • 101

1 Answers1

-1

Do you want to be applied to all your Grid controls ? If so, just modify your App.Xaml to this so you override the previous Grid style:

<Application x:Class="Wormholes.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Wormholes"
         StartupUri="Views/TitleWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type Grid}" **BasedOn="{StaticResource {x:Type Grid}}"**>
        <Setter Property="Background" Value="Black"/>
    </Style>
</Application.Resources>


If it's just for a few Grid controls modify your App.xaml to this:

<Application x:Class="Wormholes.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Wormholes"
         StartupUri="Views/TitleWindow.xaml">
<Application.Resources>
    <Style x:Key="YourGridStyleKey" TargetType="Grid">
        <Setter Property="Background" Value="Black"/>
    </Style>
</Application.Resources>

And then in your View:

<Window x:Class="Wormholes.Views.TitleWindow"
    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:Wormholes"
        xmlns:commands="clr-namespace:Wormholes.Commands"
    mc:Ignorable="d"
    Title="Warning: Weird Wormholes!" Height="450" Width="800" WindowState="Maximized" WindowStyle="None">xi
<Grid Style="{StaticResource YourGridStyleKey}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="64"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image Source="/Images/Splash.png" Grid.RowSpan="2" Grid.ColumnSpan="3"/>
    <Button Grid.Row="1" Grid.Column="0" Command="commands:Commands.StartCommand">Start</Button>
    <Button Grid.Row="1" Grid.Column="2" Command="commands:Commands.ExitCommand">Exit</Button>
</Grid>

  • 1
    I want it to apply to all grids, so I used your first example; however I now get this error: `Exception: Cannot find resource named 'System.Windows.Controls.Grid'. Resource names are case sensitive.` – ekolis Sep 07 '18 at 17:37