WPF 绑定StaticResource到控件的方法
原文: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();
            
}
}

本文完整源码下载

posted on 2019-04-15 16:54 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10711565.html

WPF 绑定StaticResource到控件的方法相关推荐

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

    在WPF中使用WinForm控件方法 原文:在WPF中使用WinForm控件方法 1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,Syste ...

  2. asp.net中将数据库绑定到DataList控件的实现方法与实例代码

    解决方法1: datalist databind() 解决方法2: 查看MSDN上的详细说明资料 解决方法3: 在DataList的模板中用table表格,如: 复制代码 代码如下: <asp: ...

  3. WPF遍历当前容器中某种控件的方法

    原文:WPF遍历当前容器中某种控件的方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79 ...

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

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

  5. WPF中一个控件绑定另一个控件的属性

    原文:WPF中一个控件绑定另一个控件的属性 如同一个Grid中的一个按钮根据另一个按钮的显示与否作出不同的响应: 绑定的时候通过ElementName来指定控件 <Grid Margin=&qu ...

  6. WPF IP地址输入控件的实现

    WPF IP地址输入控件的实现 原文:WPF IP地址输入控件的实现 一.前言 WPF没有内置IP地址输入控件,因此我们需要通过自己定义实现. 我们先看一下IP地址输入控件有什么特性: 输满三个数字焦 ...

  7. SAP UI5 应用开发教程之六十二 - 基于 OData V4 的 SAP UI5 表格控件使用方法介绍试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  8. WPF自定义日期时间控件

    WPF自定义日期时间控件 一.需求分析 二.功能实现 一.需求分析 在工作中遇到的项目中,大部分软件是处于全屏运行状态,这时候就需要在软件的界面上加上日期时间那些,方便用户查看当前时间. 二.功能实现 ...

  9. 如何在wpf中使用winform控件或者winform的自定义控件

    前言 在wpf中使用winform控件或者winform的自定义控件 一.添加引用 WindowsFormsIntegration.dll System.Windows.Forms.dll 提示:这两 ...

最新文章

  1. linux内核map图
  2. java swing 注册_Java 基础【04】Swing 组件事件注册
  3. VC GDI+: error C2660: 'new' : function does not take 3 parameters
  4. Position(Static, Absolute, Relative, Fixed)
  5. alexeyab darknet 编译_【目标检测实战】Darknet—yolov3模型训练(VOC数据集)
  6. [js] axios拦截器原理是什么?
  7. 代理网关设计与实现(基于NETTY)
  8. Exchange队列优先级介绍和配置
  9. 间歇输入数据的数据处理设计模式
  10. 【清明专刊】悼念逝去老友司徒正美,致敬曾改变世界的Flash
  11. 点歌机显示歌库服务器未能连接,快速解决常见的六种KTV点歌设备突发故障
  12. 读《创业36条军规》(三)学先进 傍大款 走正道
  13. 找不到服务器或dsn错误,win10系统找不到服务器或dns错误的解决步骤
  14. 水晶报表中几种交叉表的实现方法 (作者阿泰)
  15. JAVA儿童接种系统计算机毕业设计Mybatis+系统+数据库+调试部署
  16. 运行阶段及面向对象技巧
  17. 安恒2018.10 level1思路讲解
  18. 20221014 复数、双曲复数、对偶数
  19. OV、DV、EV证书的区别
  20. android基础学习

热门文章

  1. java学习(91):System类
  2. Qt MQTT安装步骤记录
  3. java quartz 2.2.3_java – Spring 3 Quartz 2错误
  4. 利用闭包实现onclick事件传递参数
  5. 搭建一台本地json服务器
  6. CSS3中的display:grid网格布局介绍
  7. vue中有关.env;.env.development,.env.production的相关介绍
  8. foxitreadersdk 打开远程文件_一种最不为人知最简单最方便的用电脑操作手机上的文件...
  9. GDAL读取Shp问题解决:Unable to open EPSG support file gcs.csv
  10. 一个jdbc connection连接对应一个事务