WPF 之 style文件的引用

WPF 之 style文件的引用
  总结一下WPF中Style样式的引用方法。
一、内联样式:
  直接设置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等属性。
  以设置一个Botton控件的样式为例,如:

<Button Content="Button" Name="btnDemo" 
    Height="72" Width="150" Foreground="White" Background="Blue" HorizontalAlignment="Left" 
    VerticalAlignment="Top"  Margin="170,132,0,0"  />

这种方式比较简单,但是代码不能复用。

二、嵌入样式:
  在页面<Window.Resources>节点下添加样式,然后在需要的控件上设置Style属性。还是以Botton控件为例。
  1、在页面<Window.Resources>节点下添加一个Key值叫“myBtnStyle”的样式

<Window.Resources>
  <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
    <Setter Property="Height" Value="72" />
    <Setter Property="Width" Value="150" />
    <Setter Property="Foreground" Value="Red" />
    <Setter Property="Background" Value="Black" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Top" />
  </Style>
</Window.Resources>

2、 设置Botton控件的Style属性为"{StaticResource BtnStyle}"

<Button Content="Button" Name="btnDemo" Style="{StaticResource BtnStyle}"/>

TargetType="{x:Type Button}"指定了该样式适用于Botton类型的控件,Key=“myBtnStyle"如果不设置该值,则该样式将适用于所有的Botton控件,而设置了其值为“myBtnStyle”,则只用于设置了 Style=”{StaticResource myBtnStyle}"的Botton控件。这就好比CSS中的元素选择器和类选择器。
  这种方式可以使得单个页面上的控件能够复用一个样式,比第一种方式面向对象了一步。

三、外联样式:
  1、新建一个.xaml资源文件,如/Theme/Style.xaml
  2、 在Style.xaml文件里编写样式代码

  Style.xaml:
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <Style x:Key="myBtnStyle" TargetType="Button">
    <Setter Property="Height" Value="72" />
    <Setter Property="Width" Value="150" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="Blue" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Top" />
  </Style>
</ResourceDictionary>

3、在App.xaml文件的<Application.Resources>
  或者普通页面的<Window.Resources>
  或者用户控件的 <UserControl.Resources> 节点下
  添加相应的ResourceDictionary,配置引用Style.xaml:

  app.xaml:
<Application.Resources> 
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

或者MainWindow.xaml:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Theme/BtnStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<ResourceDictionary.MergedDictionaries>节点下可以添加多个资源文件
  这种方式相比前面两种使得样式和结构又更进一步分离了。
  在App.xaml引用,是全局的,可以使得一个样式可以在整个应用程序中能够复用。在普通页面中引用只能在当前页面上得到复用。

4、设置Botton控件的Style属性为"{StaticResource myBtnStyle}" 和上面的一样。

四、用C#代码动态加载资源文件并设置样式
  1、新建资源文件:同上面的1,2两步。
  2、在后台编写代码
  首先,将我们自定义的样式加载到应用程序的资源字典中。

ResourceDictionary resourceDictionary =newResourceDictionary();
Application.LoadComponent(resourceDictionary, new Uri("/PhoneApp1;component/Resources/BtnStyle.xaml", UriKind.Relative));
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

其次,为控件添加样式。

this.btnDemo.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyle"]);

转自https://www.cnblogs.com/xinaixia/p/5512535.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,我们可以使用独立资源文件(.xaml)来定义应用程序中的样式、模板、图像和字符串等资源。这些资源文件可以在多个页面或窗口中共享,并可以动态地加载和卸载,提高了应用程序的可维护性和可扩展性。 下面是一个简单的独立资源文件的示例: ```xml <!-- AppResources.xaml --> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="ButtonBackgroundBrush" Color="#FF006699" /> <SolidColorBrush x:Key="ButtonForegroundBrush" Color="White" /> <Style TargetType="Button" x:Key="MyButtonStyle"> <Setter Property="Background" Value="{StaticResource ButtonBackgroundBrush}" /> <Setter Property="Foreground" Value="{StaticResource ButtonForegroundBrush}" /> <Setter Property="FontSize" Value="16" /> <Setter Property="Padding" Value="10,5" /> <Setter Property="Margin" Value="5" /> </Style> </ResourceDictionary> ``` 在上面的示例中,我们定义了两个 SolidColorBrush 类型的资源,分别用于按钮的背景和前景色。同时,我们还定义了一个名为 MyButtonStyle 的按钮样式,它使用了刚才定义的两个资源。 要在应用程序中使用这个独立资源文件,可以在 App.xaml 文件引用它,如下所示: ```xml <!-- 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" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="AppResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> ``` 在上面的示例中,我们将 AppResources.xaml 文件引用到了 Application.Resources 中,并使用了 MergedDictionaries 属性来合并多个资源文件。这样,在应用程序的任何页面或窗口中,都可以使用 AppResources.xaml 中定义的资源了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值