原文:WPF 绑定StaticResource到控件的方法

资源文件内的属性能否直接通过绑定应用到控件?答案是肯定的。

比如,我们要直接把下面的<SolidColorBrush x:Key="RedBrush" Color="#FFFF0000" />直接绑定到一个TextBlock的Foreground属性。

<Application x:Class="StaticResourcesWithBinding.App"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="Window1.xaml">

    <Application.Resources>
<SolidColorBrush x:Key="RedBrush" Color="#FFFF0000" />

    </Application.Resources>

</Application>

办法是直接把资源文件内的Key的名字绑定到控件,

public class MyViewModel
{
public string MyResourceKey { get; private set; }
public MyViewModel(string myResourceKey)
{
MyResourceKey = myResourceKey;
}
}

直接绑定可以吗?

<Window x:Class="StaticResourcesWithBinding.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Title="Window1" Width="400" Height="200">
<Grid>
<TextBlock Text="Hello World" FontSize="48" Foreground="{StaticResource  {Binding MyResourceKey}}" />
</Grid>

</Window>

但这样是行不通的。

必须要用下面的类进行转换

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup; namespace StaticResourcesWithBinding
{
public class BindableStaticResource : StaticResourceExtension
{
private static readonly DependencyProperty DummyProperty; static BindableStaticResource()
{
DummyProperty = DependencyProperty.RegisterAttached("Dummy",
typeof(Object),
typeof(DependencyObject),
new UIPropertyMetadata(null));
} public Binding MyBinding { get; set; } public BindableStaticResource()
{
} public BindableStaticResource(Binding binding)
{
MyBinding = binding;
} public override object ProvideValue(IServiceProvider serviceProvider)
{
var target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
var targetObject = (FrameworkElement)target.TargetObject; MyBinding.Source = targetObject.DataContext;
var DummyDO = new DependencyObject();
BindingOperations.SetBinding(DummyDO, DummyProperty, MyBinding); ResourceKey = DummyDO.GetValue(DummyProperty); return ResourceKey != null ? base.ProvideValue(serviceProvider) : null;
}
public new object ResourceKey
{
get
{
return base.ResourceKey;
}
set
{
if (value != null)
{
base.ResourceKey = value;
}
}
}
}
}

然后,我们就可以通过以下方式绑定了:

<Window x:Class="StaticResourcesWithBinding.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:StaticResourcesWithBinding="clr-namespace:StaticResourcesWithBinding"

    Title="Window1" Width="400" Height="200">
<Grid>
<TextBlock Text="Hello World" FontSize="48" Foreground="{StaticResourcesWithBinding:BindableStaticResource {Binding MyResourceKey}}" />
</Grid>

</Window>

后面的代码:

public partial class Window1
{
public Window1()
{

            this.DataContext = new MyViewModel("RedBrush");
InitializeComponent();

            
}
}

本文完整源码下载

WPF 绑定StaticResource到控件的方法的更多相关文章

  1. [转]在WPF中使用WinForm控件方法

    本文转自:http://blog.csdn.net/lianchangshuai/article/details/6415241 下面以在Wpf中添加ZedGraph(用于创建任意数据的二维线型.条型 ...

  2. WPF中TreeView控件SelectedItemChanged方法的MVVM绑定

    问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...

  3. CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)

    事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便支持wpf的开发,同时,框架仍保留最低.net framework2.0 ...

  4. CYQ.Data 支持WPF相关的数据控件绑定.Net获取iis版本

    CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09) 事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便 ...

  5. 如何在双向绑定的Image控件上绘制自定义标记(wpf)

    我们的需求是什么? 答:需要在图片上增加一些自定义标记,例如:2个图片对比时,对相同区域进行高亮. 先上效果图: 设计思路 1.概述 1.通过TargeUpdated事件,重新绘制图片进行替换. 2. ...

  6. 封装:WPF中可以绑定的BindPassWord控件

    原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...

  7. WPF 将数据源绑定到TreeView控件出现界面卡死的情况

    首先来谈一下实现将自定义的类TreeMode绑定到TreeView控件上的一个基本的思路,由于每一个节点都要包含很多自定义的一些属性信息,因此我们需要将该类TreeMode进行封装,TreeView的 ...

  8. WPF编程,将控件所呈现的内容保存成图像的一种方法。

    原文:WPF编程,将控件所呈现的内容保存成图像的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/detai ...

  9. WPF利用通过父控件属性来获得绑定数据源RelativeSource

    WPF利用通过父控件属性来获得绑定数据源RelativeSource   有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中 ...

随机推荐

  1. leetCode题解之旋转数字

    1.题目描述 X is a good number if after rotating each digit individually by 180 degrees, we get a valid n ...

  2. nodejs+redis使用

    安装 linux安装及配置之前写过了http://www.cnblogs.com/zycbloger/p/6226682.html windows安装 下载地址:https://github.com/ ...

  3. aws rhel 7 安装GUI ,配置VNC

    预计阅读时间:15分钟 预计配置时间:30分钟  (前提是已经申请AWS的EC2的rhel7 云主机并且成功运行) 目前AWS 亚马逊云免费试用一年,申请一个学习使用 痛点:没有GUI,无法搭建Jen ...

  4. 铁乐学Python_day05-字典dict

    1.[字典dict] Python内置了字典:dict的支持,dict全称dictionary, 在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 字典和列表直观上 ...

  5. C# Socket编程 笔记,Socket 详解,入门简单

    目录 一,网络基础 二,Socket 对象 三,Bind() 绑定与 Connect() 连接 四,Listen() 监听请求连接 和 Accept() 接收连接请求 五,Receive() 与 Se ...

  6. RAC数据库的RMAN备份异机恢复到单节点数据库

    1.首先在rac环境用rman备份数据库.[oracle@rac1 admin]$ rman target /run{allocate channel c1 device type disk conn ...

  7. 1251. 序列终结者【平衡树-splay】

    Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...

  8. ElasticSearch 获取es信息以及索引操作

    检查集群的健康情况 GET /_cat/health?v green:每个索引的primary shard和replica shard都是active状态的yellow:每个索引的primary sh ...

  9. ubuntu16.04安装oracle常见问题

    报错信息: Err:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libdrm2 amd64 2.4.83-1~16. ...

  10. Week4:Neural Networks难点记录

    为什么θ的维度是Sj+1*(Sj+1)? 课堂PPT没有两层单元个数不同的状态,故举一个例子就知道了 and sj+1=4, so sj+1×(sj+1)=4×3. 编程作业: Multi-class ...