在App.xaml中为Grid设置样式时,我的WPF UI消失了

时间:2018-09-06 13:40:32

标签: wpf xaml

我有一个带有网格的窗口,其中包含图像和一些按钮:

<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>

还有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>

但是,当App.xaml中存在这种样式时,那么当我运行我的应用程序时,只要打开窗口,图像和按钮就会消失!如果删除样式,则会显示控件(当然是未样式化的)。并且如果将样式放在Window.Resources中,则可以使用,但是当然,它不适用于我创建的任何其他视图。如何通过App.xaml使样式起作用?

1 个答案:

答案 0 :(得分:-1)

您是否要应用于所有Grid控件?如果是这样,只需将您的App.Xaml修改为此,以覆盖以前的Grid样式:

<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>


如果仅用于几个Grid控件,请将您的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 x:Key="YourGridStyleKey" TargetType="Grid">
        <Setter Property="Background" Value="Black"/>
    </Style>
</Application.Resources>

然后在您的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>