为什么在这种情况下无法解析StaticResource? [英] Why the StaticResource cannot be resolved in this case?

查看:951
本文介绍了为什么在这种情况下无法解析StaticResource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例外情况找不到名为'mrg'的资源.资源名称区分大小写." 当我尝试执行以下操作时:

I have got an exception "Cannot find resource named 'mrg'. Resource names are case sensitive." when I try to do the following:

MainWindow.xaml:

MainWindow.xaml:

<Window.Resources>
  <Thickness Left="0"
             Right="1"
             Bottom="2"
             Top="3"
             x:Key="mrg" />
</Window.Resources>
<Grid>
  <ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <local:UserControl1 />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

MainWindow.xaml.cs:

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> source = new List<string>()
        {
            "item1",
            "item2",
            "item3",
            "item4",
            "item5",
        };
        DataContext = source;
    }
}

和UserControl1.xaml:

and UserControl1.xaml:

<Grid>
    <TextBlock Text="{Binding}" Margin="{StaticResource mrg}" />
</Grid>

根据 msdn文章 :

静态资源查找行为

  1. 查找过程在设置属性的元素定义的资源字典中检查请求的键.

  1. The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.

然后,查找过程将逻辑树向上遍历到父元素及其资源字典.这一直持续到到达根元素为止.

The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.

接下来,检查应用程序资源.应用程序资源是资源字典中由WPF应用程序的Application对象定义的那些资源.

Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.

因此必须由于步骤2而找到资源.但是,正如我在捕获异常的Locals窗口中所见的那样,UserControl1.Parent == null.

So the resource had to be found because of step 2. But, as I can see in the Locals window when exception is catched, the UserControl1.Parent == null.

我对这个问题感到困惑.我可以解决的方法是将资源放入应用程序级别.

I'm confused in this problem. The way I can solve it is to put the resource to the Application level.

我的问题是:为什么找不到StaticResource?

My question is: why the StaticResource connot be found ?

推荐答案

DataTemplate形成自己的逻辑树,该逻辑树与ItemsControl的逻辑树断开连接.因此,遍历逻辑树的查找将找不到资源.

The DataTemplate forms a logical tree of its own, which is disconnected from the logical tree of the ItemsControl. Hence the lookup by traversing the logical tree won't find the resource.

我无法在MSDN中找到参考,只是

I wasn't able to find a reference in MSDN, just this article on CodeProject, where it reads:

属于扩展模板的元素,以下称为 作为模板元素",形成自己的逻辑树 与它们所在的对象的逻辑树断开连接 已创建.

The elements that are part of an expanded template, hereafter referred to as "template elements", form their own logical tree which is disconnected from the logical tree of the object for which they were created.


使用DynamicResource代替StaticResource将克服此问题.但是我不能确切地说出原因.也许可以在


Using DynamicResource instead of StaticResource will overcome the problem. However i can't tell exactly why. Maybe an explanation can be found in the Static resource lookup behavior and Dynamic resource lookup behavior sections in Static and Dynamic Resources, but i'm not sure.

这篇关于为什么在这种情况下无法解析StaticResource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆