WPF 基础入门(样式)

3.1 一般样式

<Grid Margin="10">
    <TextBlock Text="Style test" Foreground="Red" FontSize="20"/>
</Grid>

3.2内嵌样式

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

  <Grid Margin="10">
        <TextBlock Text="Style test">
            <TextBlock.Style>
                <Style>
                    <Setter Property="TextBlock.FontSize" Value="36" />
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>

3.3父资源样式

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

  <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窗口下面的资源样式,当前窗口下样式起作用。

    <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中的,针对整个项目,都是可以用的

<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全局引用

<Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Red" />
      <Setter Property="FontSize" Value="24" />
</Style>

3.6.2 静态引用

<RadioButton Style="{StaticResource BtnRadioButton}"/>

3.6.3动态引用

<RadioButton Style="{DynamicResource BtnRadioButton}"/>

3.7样式引用顺序

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

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

相关推荐

  1. WPF 基础入门样式

    2023-12-31 17:42:02       63 阅读
  2. WPF 基础入门 (触发器)

    2023-12-31 17:42:02       59 阅读
  3. WPF 基础入门 (Binding 一)

    2023-12-31 17:42:02       54 阅读
  4. WPF 基础入门(XAML理解二)

    2023-12-31 17:42:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-31 17:42:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 17:42:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 17:42:02       82 阅读
  4. Python语言-面向对象

    2023-12-31 17:42:02       91 阅读

热门阅读

  1. 什么是ajax,为什么使用ajax?

    2023-12-31 17:42:02       68 阅读
  2. ssh连接docker与宿主机进入docker环境变量不一致

    2023-12-31 17:42:02       73 阅读
  3. 多开工具对手机应用启动速度的优化与改进

    2023-12-31 17:42:02       60 阅读
  4. 配置yum镜像源

    2023-12-31 17:42:02       46 阅读
  5. 速盾网络:cdn数量对网站访问速度的影响

    2023-12-31 17:42:02       61 阅读
  6. 2023年广东省网络安全B模块(笔记详解)

    2023-12-31 17:42:02       44 阅读
  7. 使用Python绘制各种图表

    2023-12-31 17:42:02       61 阅读
  8. python随机生成数字random模块

    2023-12-31 17:42:02       65 阅读
  9. 成立一个理解起来很直观的 cpu cmodel 项目

    2023-12-31 17:42:02       60 阅读