WPF整理--动态绑定到Logical Resource
2024-10-12 13:27:44
“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>
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;
}
}
}
最新文章
- Windows 7远程桌面连接Ubuntu 16.04
- 网站安全通用防护代码(C#版本源码提供)
- Java自定义日志输出文件
- 用C++的类做三种优先队列的实现
- Linux网络配置命令ifconfig输出信息解析
- 如何打开mo文件并修改 PoEdit
- ORACLE 热备begin backup / end backup
- Linux中搭建SVN服务器
- [MYSQL] 记一次MySQL性能调优
- libevent之eventop
- redis 主从配置,主从切换
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D	Grid(简单构造)
- Batchnorm
- TZOJ 2519 Regetni(N个点求三角形面积为整数总数)
- Charles 学习笔记
- 利用captcha库绘制验证码
- 【转载】LCT
- js-设计模式学习笔记-策略模式
- 网易云易盾朱星星:最容易被驳回的10大APP过检项
- python-条件和循环
热门文章
- Linux进程间通信(八):流套接字 socket()、bind()、listen()、accept()、connect()、read()、write()、close()
- Linux 下测试网卡性能命令iperf 的用法
- nginx error_log 错误日志配置说明
- Sql Server 2008服务启动失败,错误17058
- .NET 面向对象基础
- 数据库DBA(3年以内需求)
- js随机数
- 情人节那点事,Power BI告诉你
- UWP 解决Webview在Pivot里面无法左右滑动的问题
- wenbenfenlei