WPF 基础入门(样式)

3.1 一般样式

XML 复制代码
<Grid Margin="10">
    <TextBlock Text="Style test" Foreground="Red" FontSize="20"/>
</Grid>

3.2内嵌样式

直接在控件上定义样式,如下所示:

XML 复制代码
  <Grid Margin="10">
        <TextBlock Text="Style test">
            <TextBlock.Style>
                <Style>
                    <Setter Property="TextBlock.FontSize" Value="36" />
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>

3.3父资源样式

使用控件的Resources部分,可以面向此控件的子控件(以及这些子控件的子控件等)。

XML 复制代码
  <Grid Margin="10">
        <Grid.Resources>
            <Style  TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="FontSize" Value="24" />
            </Style>
        </Grid.Resources>
        <TextBlock Text="Style test"/>
    </Grid>

3.4窗口资源样式

写在Window窗口下面的资源样式,当前窗口下样式起作用。

XML 复制代码
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="FontSize" Value="24" />
        </Style>
    </Window.Resources>
    <Grid Margin="10">
        <TextBlock Text="Style test"/>
    </Grid>

3.5应用程序资源样式

写在应用程序资源App.xaml中的,针对整个项目,都是可以用的

XML 复制代码
<Application x:Class="ImageTestTool.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary
            </ResourceDictionary.MergedDictionaries>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="FontSize" Value="24" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3.6样式引用

3.6.1全局引用

XML 复制代码
<Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Red" />
      <Setter Property="FontSize" Value="24" />
</Style>

3.6.2 静态引用

XML 复制代码
<RadioButton Style="{StaticResource BtnRadioButton}"/>

3.6.3动态引用

XML 复制代码
<RadioButton Style="{DynamicResource BtnRadioButton}"/>

3.7样式引用顺序

控件属性<-控件内样式<-父控件资源<-本窗体资源样式<-应用程序资源样式

**************************************************************************************************************

相关推荐
凌镜icecraft2 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
光泽雨7 天前
控件Password附加属性
wpf
samble11 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble20 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴21 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67522 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教22 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨24 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟25 天前
WPF(数据绑定)
wpf
samble1 个月前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例