【问题标题】:How to apply style on a window in Xaml如何在 Xaml 中的窗口上应用样式
【发布时间】:2017-03-23 23:02:06
【问题描述】:

我尝试在 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>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您可以直接将其分配给Window的Style属性,而不是将其放入Window的Resources

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

    【讨论】:

      【解决方案2】:

      您必须将您的样式放在 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 本身。

      【讨论】:

      • 是的,我尝试使用 ,它将样式应用于整个项目中的所有页面。谢谢!
      猜你喜欢
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多