asked    Joyce     2018-10-22       wpf       127 view        1 Answer

[SOLVED] My WPF UI disappears when I set a style for Grid in App.xaml

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?

  1 Answer  

        answered    June     2018-10-22      

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>





Your Answer





 2018-10-22         Rock

Please clarify my mistake while doing the dry run

The following code is all correct but I have a doubt. While I am doing the dry run I find in my mind that it should show 2 as NOT a prime number. Someone please clarify how the code is running all good. num = 4if num > 1: for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number")else: print(num,"is not a prime number") 2 is a prime number, but your code will shows 2 as not a prime numberHere is the fix:num = 4if num > 2: fo...
 python                     1 answers                     55 view
 2018-10-22         Merry

Calculate the area under each peak in a graph in python

I am trying to calculate the area under each peak in a graph that I plotted with a set of x and y co-ordinates, I don't have a function for (x,y), and so I haven't been able to find an appropriate method to do the same.the co-ordinates are { [10 10] [11 1] [12 7] [14 4] [16 8] [17 5]]}And y=0 for all the unmarked x values The area below two points consists of a 'square' part and a triangular part. The trick is to calculate them both separately and add them. Do this for all points, and you get a list of areas under peaks:points = [[10, 10], [11, 1], ...
 python                     3 answers                     55 view
 2018-10-22         Jeff

Time Complexity of while loop

What is the Best case, Worst Case and Average Case scenario of the followingcode segment ? def sumInts(n): Count = 0 while i < n: Count = count + n Return count It doesn't run because there's neither an i nor a count defined anywhere.In other words: Best case: crash, Worst case: crash, Average case: crash. [XXX]
 python                     1 answers                     57 view