Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Mysql

    • mysql基本使用
    • 总结下mysql8在windows环境下的安装和配置
    • Mysql常用函数
    • Mysql常用语句
  • PHP

  • Python

  • java

  • Go

  • 数据库

  • C#

  • ElasticSearch

  • Git

  • Gulp

  • Microsoft

  • Linux

  • 其它

  • 技术
  • C#
  • wpf
mrcdh
2023-12-12
目录

wpf资源基础

# 动态资源和静态资源

<Window.Resources>
    <SolidColorBrush x:Key="SolidColor" Color="Red" />
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Margin="10" Content="update" Click="Button_Click"></Button>
        <Button Margin="10" Content="button1" BorderBrush="{StaticResource SolidColor}"></Button>
        <Button Margin="10" Content="button2" BorderBrush="{DynamicResource SolidColor}"></Button>
    </StackPanel>
</Grid>
1
2
3
4
5
6
7
8
9
10
this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
1
  • StaticResource: 引入静态资源,目标发生改变不会跟着变
  • DynamicResource: 引入动态资源,目标发证改变会跟着变

# 资源字典

右键添加 -> 资源字典 -> ButtonStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="DefualtButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="FontSize" Value="15"/>
    </Style>
</ResourceDictionary>
1
2
3
4
5
6
7

全局引用App.xaml

<Application.Resources>
    <!-- 资源字典 -->
    <ResourceDictionary>
        <!-- 字典集合 -->
        <ResourceDictionary.MergedDictionaries>
            <!-- 追加地点 -->
            <ResourceDictionary Source="ButtonStyle.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
1
2
3
4
5
6
7
8
9
10

布局中直接使用即可

<Button Margin="10" Style="{StaticResource DefualtButtonStyle}" Content="button1" />
1

查找字典

var style = App.Current.FindResouce("DefualtButtonStyle");
1
#wpf
上次更新: 2023/12/12, 21:55:41
最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×