WPF 之 style文件的引用

本文详细介绍了WPF中样式(Style)的四种引用方法:内联样式、嵌入样式、外联样式及C#代码动态加载。每种方法均有实例说明,帮助读者理解如何在不同场景下有效复用样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

### 如何在 WPF引用 Style 资源 #### 在应用程序级别定义并引用样式资源 当希望在整个应用范围内使用某个 `Style` 时,可以在应用程序级别的资源字典中定义该 `Style`。这通常是在 App.xaml 文件内完成的: ```xml <Application.Resources> <!-- 定义全局可用的样式 --> <Style TargetType="Button" x:Key="GlobalButtonStyle"> <Setter Property="Background" Value="LightBlue"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="FontSize" Value="14"/> </Style> </Application.Resources> ``` 一旦定义好之后,在任何页面或窗口里都可以通过键名来引用这个样式[^1]。 #### 页面或控件局部范围内的样式定义与引用 对于仅限于特定页面或控件使用的 `Style`,可以直接将其放置在对应的 `<Window>` 或者其他容器元素下的 Resources 部分: ```xml <Window.Resources> <!-- 局部定义按钮样式 --> <Style TargetType="Button" x:Key="LocalButtonStyle"> <Setter Property="Background" Value="Green"/> <Setter Property="FontWeight" Value="Bold"/> </Style> </Window.Resources> <!-- 使用本地定义好的样式 --> <Button Content="Click Me!" Style="{StaticResource LocalButtonStyle}" /> ``` 这里展示了如何在一个具体的 Window 下创建名为 "LocalButtonStyle" 的 Button 样式,并且立即应用于一个按钮实例上[^2]。 #### 动态设置样式的例子 除了静态地指定样式外,还可以利用 C# 代码动态改变 UI 元素所采用的样式。例如下面这段代码演示了怎样给 TreeViewItem 设置 ItemContainerStyle: ```csharp treeViewItem.SetValue(TreeViewItem.ItemContainerStyleProperty, Application.Current.Resources["ScriptTreeItemStyle"]); ``` 此操作允许程序运行期间根据逻辑条件灵活调整界面组件的表现形式[^3]。 #### 结合转换器使用的复杂场景 有时候可能还需要更复杂的定制化需求,比如基于某些业务规则修改显示文本的内容。这时就可以借助绑定机制配合自定义的数据转换器 (IValueConverter) 来实现这样的功能。如下所示的例子说明了如何将一个字符串类型的属性值经过 ReplaceChar 类型的 IValueConverter 处理后再赋给 TextBlock 控制的文字内容: ```xml <TextBlock Width="96" Height="16" TextAlignment="Right"> <TextBlock.Style> <Style TargetType="TextBlock"> <Setter Property="Text"> <Setter.Value> <Binding Path="SomeStringProperty"> <Binding.Converter> <local:ReplaceChar/> </Binding.Converter> </Binding> </Setter.Value> </Setter> </Style> </TextBlock.Style> </TextBlock> ``` 上述片段中的 `local:` 前缀应当指向包含有 ReplaceChar 实现类所在的命名空间声明处[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值