WPF-资源基础

静态资源和动态资源

静态资源:有动作也不会改变原本的状态(StaticResource SolidColor)

动态资源:可以根据相应的动作来变换姿态(DynamicResource SolidColor)

单击按钮实现按钮边框颜色变化

    <Window.Resources>
        <SolidColorBrush x:Key="add"
                         Color=" red" />
    </Window.Resources>


    <Grid>
        <StackPanel>
            <Button Content="sb1"
                    BorderBrush="{StaticResource add}"
                    Click="Button_Click"
                    Height="50"
                    Width="118"
                    BorderThickness="5">
                <Button.Background>
                    <ImageBrush ImageSource="/th.jpg" />
                </Button.Background>
            </Button>


            <Button Content="sb2"
                    BorderBrush="{StaticResource add}"
                    Height="52"
                    Width="120"
                    BorderThickness="5">
                <Button.Background>
                    <ImageBrush ImageSource="/R-C.jpg" />
                </Button.Background>
            </Button>
            <Button Content="sb3"
                    BorderBrush="{DynamicResource add}"
                    Height="56"
                    Width="124"
                    BorderThickness="5">
                <Button.Background>
                    <ImageBrush ImageSource="/微信图片_20220320014343.png" />
                </Button.Background>
            </Button>
        </StackPanel>
    </Grid>
namespace 项目练习
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["add"] = new SolidColorBrush(Colors.Green); 
        }
    }
}

 按下之后边框变颜色了 就是因为第三个使用的动态资源引用 所以能变化 则第二个静态没有变化

 资源字典

资源字典:资源字典中的资源要添加到界面当中,在App里面添加

 创建资源字典

 然后要在App.xaml里添加我们的资源,然后才能在外面引用

 然后就可以在任意窗口引用资源了 按下F10 在style可以查看可引用的资源

namespace 项目练习
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["add"] = new SolidColorBrush(Colors.Green);

            var solidColor = App.Current.FindResource("add");

            var style = App.Current.FindResource("Buttonstyle");
        }
    }
}

效果和前面的一样 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值