WPF整理--动态绑定到Logical Resource

简介: 原文:WPF整理--动态绑定到Logical Resource“What happens if we replace aspecific resource? Would that be reflected in all objects using the resource? And if not,...
原文: WPF整理--动态绑定到Logical Resource

What happens if we replace a
specific resource? Would that be reflected in all objects using the resource? And if not, can we
bind to the resource dynamically?”

接上篇博文,如果我们需要在C#代码中动态替换某个logical resource,我们该如何做呢?

如,Window的Resources属性赋值如下:

<Window x:Class="DynamicallyBindingToALogicalResource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <LinearGradientBrush x:Key="brush1">
            <GradientStop Offset="0.3" Color="Yellow"/>
            <GradientStop Offset="0.7" Color="Brown"/>
        </LinearGradientBrush>
    </Window.Resources>
    <StackPanel>        
        <Rectangle Height="100" Stroke="Pink" StrokeThickness="20" Fill="{StaticResource brush1}"/>        
        <Button x:Name="button1" Content="Replace Brush" Height="100" Click="OnReplaceBrush"/>
    </StackPanel>
</Window>

Button Click事件中Replace Window的key为"brush1"的Resources

        private void OnReplaceBrush(object sender, RoutedEventArgs e)
        {
            var brush = new LinearGradientBrush();
            brush.GradientStops.Add(new GradientStop(Colors.Pink, 0.2));
            brush.GradientStops.Add(new GradientStop(Colors.Blue, 0.5));
            brush.GradientStops.Add(new GradientStop(Colors.Violet, 0.8));
            
            this.Resources["brush1"] = brush;
        }

点击按钮,程序没有任何反应,可以通过DynamicResource这个markup extension实现。

Run the application and click on the button. You'll notice nothing happens. Now
change the StaticResource markup extension to DynamicResource on the
Rectangle:”

<Rectangle Height="100" Stroke="Violet" StrokeThickness="20" Fill="{DynamicResource brush1}" />


程序运行如下:

点击Button后:

附XAML和C#代码:

<Window x:Class="DynamicallyBindingToALogicalResource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <LinearGradientBrush x:Key="brush1">
            <GradientStop Offset="0.3" Color="Yellow"/>
            <GradientStop Offset="0.7" Color="Brown"/>
        </LinearGradientBrush>
    </Window.Resources>
    <StackPanel>        
        <Rectangle Height="100" Stroke="Pink" StrokeThickness="20" Fill="{StaticResource brush1}"/>
        <Rectangle Height="100" Stroke="Violet" StrokeThickness="20" Fill="{DynamicResource brush1}" />
        <Button x:Name="button1" Content="Replace Brush" Height="100" Click="OnReplaceBrush"/>
    </StackPanel>
</Window>
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DynamicallyBindingToALogicalResource
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnReplaceBrush(object sender, RoutedEventArgs e)
        {
            var brush = new LinearGradientBrush();
            brush.GradientStops.Add(new GradientStop(Colors.Pink, 0.2));
            brush.GradientStops.Add(new GradientStop(Colors.Blue, 0.5));
            brush.GradientStops.Add(new GradientStop(Colors.Violet, 0.8));
            
            this.Resources["brush1"] = brush;
        }
    }
}
View Code

 

目录
相关文章
|
7月前
|
存储 安全 程序员
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
16 0
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
|
C# .NET 开发框架
WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新
原文:WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新 1、xmlns Mapping URI的格式是 clr-namespace:[;assembly=] (1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
1402 0
|
C#
WPF在DLL中读取Resource的方法
原文:WPF在DLL中读取Resource的方法 WPF是个用户控件,被WinForm调用。而WinForm是在一个DLL类库中被调用。试了很多方法,都无法将Resource中的图读进程序。用下面的方法总算实现了。
1753 0
|
C# 前端开发
WPF 用代码调用dynamic resource动态更改背景 - CSDN博客
原文:WPF 用代码调用dynamic resource动态更改背景 - CSDN博客 一般dynamic resoource通常在XAML里调用,如下范例: 如果在ResourceDictionary定义了很...
1814 0
|
C#
How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behind-in-wpf 如何在WPF后台代码中中复制引用的资源? In my application, I have a color resources.
801 0
WPF添加具有相对路径的Component Resource
如下图,想给Image添加一个Source Voice.png 按理说,在【1】处应该出现一个资源文件并且以相对路径表示,而现在【2】中出现的竟然是绝对路径! 什么原因? 检查一下解决方案下的[Recources/WeiXin],发现Voice.png已经在里面。
903 0
|
存储 C# Windows
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
原文:2000条你应知的WPF小姿势 基础篇   在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师。最为出色的是他维护了两个博客:2,000Things You Should Know About C#  和 2,000 Things You Should Know About WPF 。
1079 0
|
1月前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库