如何在Xaml中为窗口应用样式

3

我尝试在Xaml中为窗口应用样式,但我的代码没有应用样式。有人能帮我解决这个问题吗?

<Window x:Class="Shweta.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window5" Height="300" Width="300">

    <Window.Resources>
        <Style  TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Verdana"></Setter>
            <Setter Property="Foreground" Value="LightBlue"></Setter>
            <Setter Property="FontWeight" Value="Normal"></Setter>
            <Setter Property="WindowStyle" Value="None"></Setter>
            <Setter Property="Background" Value="LightBlue"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40*" />
            <ColumnDefinition Width="238*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="47*" />
            <RowDefinition Height="214*" />
        </Grid.RowDefinitions>

        <Grid Grid.Column="1">
            <Button Margin="0,0,114,16" Content="shweta"/>
        </Grid>
    </Grid>
</Window>
2个回答

3

不要将样式放入窗口的资源中,而是可以直接将其分配给窗口的Style属性:

<Window x:Class="Shweta.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window5" Height="300" Width="300">
    <Window.Style>
        <Style TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="Verdana"/>
            <Setter Property="Foreground" Value="LightBlue"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="WindowStyle" Value="None"/>
            <Setter Property="Background" Value="LightBlue"/>
        </Style>
    </Window.Style>
    ...
</Window>

0

你需要将你的样式放在 App.xaml 的 Resources 中。

<Application.Resources>
    <Style  TargetType="{x:Type Window}">
        <Setter Property="FontFamily"
                Value="Verdana"></Setter>
        <Setter Property="Foreground"
                Value="LightBlue"></Setter>
        <Setter Property="FontWeight"
                Value="Normal"></Setter>
        <Setter Property="WindowStyle"
                Value="None"></Setter>
        <Setter Property="Background"
                Value="LightBlue"></Setter>
    </Style>
</Application.Resources>

当您在Window中定义Style时,它将适用于Window的子元素,但不包括Window本身。


是的,我尝试使用 <application.resources>,它适用于整个项目中的所有页面。谢谢! - shweta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接