当前位置: 首页 > news >正文

wpf DynamicResource的ResourceKey值进行绑定

wpf 中的DynamicResource 的ResourceKey不支持绑定的,因为它不是个DependencyProperty,但又想动态指定ResourceKey
场景:
Name属性有多个值 zhangsan,lisi,wangwu
对应不同资源文件中的

//  style1<sys:String x:Key="zhangsan">张三</sys:String><sys:String x:Key="lisi">李四</sys:String><sys:String x:Key="wangwu">王五</sys:String>
// 另一个文件中的 style2        <sys:String x:Key="zhangsan">ZhangSan</sys:String><sys:String x:Key="lisi">LiSi</sys:String><sys:String x:Key="wangwu">WangWu</sys:String>

实现动态改变Name的同时进行动态资源绑定

<ItemsControl ItemsSource="{Binding StudentList}"><ItemsControl.ItemTemplate><HierarchicalDataTemplate><TextBlock><TextBlock.Text><MultiBinding Converter="{StaticResource dynBindingConv}" ConverterParameter="TextProperty"><Binding Path="Name"/><Binding RelativeSource="{RelativeSource Self}"/></MultiBinding></TextBlock.Text></TextBlock></HierarchicalDataTemplate></ItemsControl.ItemTemplate></ItemsControl>

可以通过Converter实现,stackoverflow上有个实现方法点击跳转
但工作量太大下面是简陋但能实现效果的方法
xaml:

<converter:DynamicBindingConverter x:Key="dynBindingConv"/>

<TextBlock><TextBlock.Text><MultiBinding Converter="{StaticResource dynBindingConv}" ConverterParameter="TextProperty"><Binding Path="Name"/><Binding RelativeSource="{RelativeSource Self}"/></MultiBinding></TextBlock.Text>
</TextBlock>
public class DynamicBindingConverter: IMultiValueConverter{public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture){string key = values[0].ToString();var control = values[1] as FrameworkElement;string property = parameter.ToString();Type type = control.GetType();FieldInfo field = type.GetField(property, BindingFlags.Public | BindingFlags.Static);DependencyProperty d_property = (DependencyProperty)field.GetValue(null);var dynamicResource = new DynamicResourceExtension(key);var resourceReferenceExpression = dynamicResource.ProvideValue(null);control.SetValue(d_property, resourceReferenceExpression);return Application.Current.Resources[key];}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}}

难点主要是通过反射得到DependencyProperty

发现有的控件找不到对应属性,是因为反射只在当前类中查找不会去父类(或者基类)中查找``对上述代码用下修改即可

public class DynamicBindingConverter: IMultiValueConverter{public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture){string key = values[0].ToString();var control = values[1] as FrameworkElement;string property = parameter.ToString();Type type = control.GetType();FieldInfo field = null;while (type != null){field = type.GetField(property, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public);if (field != null){break;}type = type.BaseType;}if (field != null){DependencyProperty d_property = (DependencyProperty)field.GetValue(null);var dynamicResource = new DynamicResourceExtension(key);var resourceReferenceExpression = dynamicResource.ProvideValue(null);control.SetValue(d_property, resourceReferenceExpression);return Application.Current.Resources[key];}else{return Application.Current.Resources["none"];}}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}}

http://www.mrgr.cn/news/9861.html

相关文章:

  • 基于x86 平台opencv的图像采集和seetaface6的图像质量评估功能
  • python使用ffmpeg将视频、音频合并合成(速度最快)
  • 使用 Springdoc OpenAPI 为 Spring Boot 应用程序生成 Swagger文档
  • AI图片生成网站Midjourney网页版开放注册 每用户可免费生成25张图片
  • 考试:计算机网络(01)
  • 【碎片】FastAPI 路径参数
  • Gazebo Harmonic 和 ROS2 jazzy 安装和测试
  • 备战秋招60天算法挑战,Day24
  • 开放式耳机危害大吗?6点如何挑选合适的开放式耳机!
  • 嵌入式面经篇十——驱动开发
  • 【hot100篇-python刷题记录】【搜索二维矩阵】
  • [JAVA]什么是泛型?泛型在Java中的应用
  • C#算法之二分查找
  • python实现简单中文词元化、词典构造、时序数据集封装等
  • 【Linux】第十六章 高级IO (五种IO模型+fcntl)
  • 什么是ElasticSearch的深度分页问题?如何解决?
  • NRK3301语音识别芯片在汽车内饰氛围灯上的应用方案解析
  • Vue3 获取农历(阴历)日期,并封装日历展示组件
  • 泰国中小企业局局长率考察团到访深兰科技
  • SpringBoot天猫商城基于前后端分离+SpringBoot+BootStrap、Vue.js、JQuery+JPA+Redis