WPF基础入门-Class8-资源基础

WPF基础入门

Class8-资源基础

前言:方便各种资源的集中管理和动态效果
静态引用:初始化的时候确定样式,后续不发生改变
动态引用:样式随着引用的内容一起改变
1、新建资源字典.xaml,创建一个边框颜色资源MyBrush和一个button的style

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Style x:Key="DefaultButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Red"></Setter>
        <Setter Property="FontSize" Value="15"></Setter>
    </Style>


    <SolidColorBrush x:Key="MyBrush" Color="Yellow"/>

</ResourceDictionary>

2、在App.xaml中引入资源字典

<Application.Resources>
         <!--引入资源字典-->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ButtonStyle.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

3、新建一个页面.xaml

<Window x:Class="WPF_Learn.Class_10_资源基础"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Learn"
        mc:Ignorable="d"
        Title="Class_10_资源基础" Height="450" Width="800">
    
    <!--在App.xaml里面引入资源字典-->
    
    <Grid>
        <StackPanel>
            <Button Content="点击我触发Test_Click" Click="Test_Click" Style="{StaticResource DefaultButtonStyle}"></Button>
            <Button Content="静态引用,不会发生改变" Margin="0,5,0,5" BorderBrush="{StaticResource MyBrush}"></Button>
            <Button Content="动态引用,点击按钮MyBrush改变后样式跟着改变" BorderBrush="{DynamicResource MyBrush}"></Button>
        </StackPanel>
    </Grid>
</Window>
 private void Test_Click(object sender, RoutedEventArgs e)
        {
            // 资源的动态改变 静态引用在初始化的时候固定
            this.Resources["MyBrush"] = new SolidColorBrush(Colors.Blue);
            
			//寻找资源字典
            //var solidColor = App.Current.FindResource("SolidColor");
            //var style = App.Current.FindResource("DefaultButtonStyle");
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜菜三菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值