【发布时间】:2015-11-19 10:27:01
【问题描述】:
今晚在解决动态资源问题时,我最终找到了一个解决方案,它依赖于Behavior 类参与其相关框架元素的资源层次结构的能力。例如,考虑以下
<Application>
<Application.Resources>
<system:String x:Key="TestString">In App Resources</system:String>
</Application.Resources>
</Application>
<Window>
<Window.Resources>
<system:String x:Key="TestString">In Window Resources/system:String>
</Window.Resources>
<Border>
<Border.Resources>
<system:String x:Key="TestString">In Border Resources</system:String>
</Border.Resources>
<TextBlock Text="{DynamicResource TestString}" />
</Border>
</Window>
TextBlock 将从边框显示资源。但是,如果我这样做...
public void Test()
{
var frameworkElement = new FrameworkElement();
var testString = (string)frameworkElement.FindResource("TestString");
}
...它从应用程序中找到一个,因为它不是可视化树的一部分。
也就是说,如果我改为这样做......
public class MyBehavior : Behavior<FrameworkElement>
{
public string Value... // Implement this as a DependencyProperty
}
然后像这样添加到TextBlock中...
<TextBlock Text="{DynamicResource TestString}">
<i:Interaction.Behaviors>
<local:MyBehavior Value="{DynamicResource TestString}" />
</i:Interaction.Behaviors>
</TextBlock>
行为确实获取资源的价值并将动态跟踪它。但是怎么做呢?
行为不是 FrameworkElement,因此您不能对其调用 SetResourceReference,它也不是可视树的一部分,因此即使您可以调用 SetResourceReference,它仍然无法找到 FrameworkElement 本地的资源。然而,这正是 Behavior 所做的。怎么样?
换一种说法,如果我们想编写自己的类来展示同样的行为(不是双关语),如何将自己插入到可视化树的资源层次结构中?
【问题讨论】:
标签: c# wpf behavior resourcedictionary dynamicresource