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样式引用顺序

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

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

相关推荐
wuli_滔滔2 小时前
【探索实战】深入浅出:使用Kurator Fleet实现跨云集群的统一应用分发
架构·wpf·kurator·fleet
松☆6 小时前
Flutter 与 OpenHarmony 深度集成:自定义 MethodChannel 插件开发全指南
flutter·wpf
Aevget8 小时前
界面控件DevExpress WPF中文教程:Data Grid - 虚拟源限制
hadoop·wpf·界面控件·devexpress·ui开发
听风吟丶8 小时前
Java 微服务日志实战:ELK+SkyWalking 构建全链路日志监控与智能告警体系某电商平台曾因日志问题陷入
wpf
她说彩礼65万10 小时前
WPF 样式
大数据·hadoop·wpf
她说彩礼65万10 小时前
WPF Behavior
wpf
她说彩礼65万10 小时前
WPF Binding Source
大数据·hadoop·wpf
Aevget10 小时前
界面控件DevExpress WPF v25.1新版亮点:富文本编辑器全新升级
开发语言·c#·wpf·devexpress·用户界面
张人玉12 小时前
WPF中无框架、Prism 框架、CommunityToolkit.Mvvm 框架的区别
c#·wpf·prism
张人玉1 天前
Prism 框架笔记及实例
c#·wpf·prism