[英]WPF global style for Window
我正在尝试将全局样式(字体大小和字体系列)添加到我拥有的 Window 的 WPF 应用程序中,但是无论我做什么,都没有应用任何样式。 我认为我的问题是我的启动窗口不是 App.xaml,因为我使用 App.xaml 只是为了检查用户是否有权运行应用程序。 但是在那之后我想要的窗口打开了,所以我的App.xaml 中的StartupUri被设置为那个窗口。
这是我的App.xaml
:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
StartupUri="FirstWindowToShow.xaml">
<Application.Resources>
<!--Style that should be applied to all Windows-->
<Style x:Key="Win_style" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
<!--Style for all Pages - works fine-->
<Style x:Key="PageFont" TargetType="{x:Type Page}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="12" />
</Style>
</Application.Resources>
</Application>
这是我的FirstWindowToShow.xaml
:
<Window x:Class="MyApp.FirstWindowToShow"
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:Priprava_Podatkov"
mc:Ignorable="d"
Title="Some title" Height="480" Width="800" Loaded="Window_Loaded" Background="#FFF9F9F9" OpacityMask="Black">
<Grid>
<Menu x:Name="G_Menu" HorizontalAlignment="Left" VerticalAlignment="Top" Height="20" Width="792">
<MenuItem x:Name="Menu_Program">
<MenuItem x:Name="Menu_V" Header="About" Click="Menu_V_Click"/>
<MenuItem x:Name="Menu_End" Header="Close" Click="Menu_End_Click"/>
</MenuItem>
<MenuItem Header="Department 1" Height="20" Width="148">
<MenuItem x:Name="Dept_1" Header="Custom controlling" Click="Dept_1_Click"/>
</MenuItem>
</Menu>
<Frame x:Name="Frame_s" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="772" NavigationUIVisibility="Hidden"/>
<StatusBar DockPanel.Dock="Bottom" Margin="0,386,0,0" VerticalAlignment="Bottom" Background="Transparent">
<StatusBarItem Width="73">
<Label Content="User:" FontWeight="Bold" Width="73"/>
</StatusBarItem>
<StatusBarItem>
<Label x:Name="LblU" Content="user" FontWeight="Light"/>
</StatusBarItem>
<StatusBarItem>
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Height="10" />
</StatusBarItem>
<StatusBarItem>
<Label Content="User permissions:" FontWeight="Bold" />
</StatusBarItem>
<StatusBarItem>
<Label x:Name="LblN" Content="Rights" FontWeight="Light"/>
</StatusBarItem>
<StatusBarItem >
<Label x:Name="Lbl_P" Content="Data exported..." >
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Resources>
<Storyboard x:Key="flashAnimacija">
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:1.5" RepeatBehavior="Forever" />
</Storyboard>
</Style.Resources>
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName= Progress_DoKonca, Path= IsVisible}" Value="True">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger.EnterActions>
<BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimacija}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="flash"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Right" Margin="-10,0,10,0">
<Grid>
<ProgressBar x:Name="Progress_TillEnd" Width="150" Height="20" />
<TextBlock x:Name="Progress_Txt" Text="{Binding ElementName=Progress_DoKonca, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</StatusBarItem>
</StatusBar>
</Grid>
</Window>
这是我过去所做的,所以看看它是否适合你:
在您的 App.xaml 中,从 Window 样式中删除x:Key
,使其变为:
<!--Style that should be applied to all Windows-->
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
然后在您的 App.xaml.cs(代码隐藏)中,覆盖OnStartup
方法并添加以下代码:-
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
{
DefaultValue = FindResource(typeof(Window))
});
}
这会将 App.xaml Window
样式(即 FontFamily 和 FontStyle)中的那些样式应用于应用程序创建的所有窗口。
对于像 Menu 和 StatusBar 这样的控件,有必要像下面这样显式设置样式:
<Style x:Key="BaseStyle" TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="13" />
</Style>
<Style TargetType="{x:Type StatusBar}" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="{x:Type Menu}" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="{x:Type local:Window1}" BasedOn="{StaticResource BaseStyle}" />
为什么将x:Key
为“Win_style”的Style
应用于所有窗口?
您可以保留Win_style
并定义每个基于Win_style
窗口类型(例如FirstWindowToShow
)的隐Style
:
<Application.Resources>
<!--Style that should be applied to all Windows-->
<Style x:Key="Win_style" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="14" />
</Style>
<!-- implicit window styles, one for each window -->
<Style TargetType="{x:Type local:FirstWindowToShow}" BasedOn="{StaticResource Win_style}" />
<!--Style for all Pages - works fine-->
<Style x:Key="PageFont" TargetType="{x:Type Page}">
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Setter Property="FontSize" Value="12" />
</Style>
</Application.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.