您的位置:首页 > 教育 > 培训 > wpf DynamicResource的ResourceKey值进行绑定

wpf DynamicResource的ResourceKey值进行绑定

2024/10/5 21:20:30 来源:https://blog.csdn.net/qq_40197926/article/details/141497497  浏览:    关键词: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){var key = values[0];var control = values[1] as FrameworkElement;var 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.ToString()];}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){var key = values[0];var control = values[1] as FrameworkElement;var 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.ToString()];}else{return Application.Current.Resources["none"];}}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com