TryFindResource 无法找到资源 [英] TryFindResource is not able to find the resource

查看:60
本文介绍了TryFindResource 无法找到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 UserControl 派生的自定义 WPF 控件.在我的 XAML 文件的资源部分,我为将在我的自定义控件上显示的项目定义了几个模板和样式.

I have a customized WPF control that is derived from UserControl. In the Resources section in my XAML file I have several templates and styles defined for the items that will be displayed on my custom control.

这是我的一种样式的定义,用于绘制向下的箭头.

Here is a definition for one of my styles to draw an downward pointing arrow.

    <Style x:Key="ArrowStyle" TargetType="Path">
        <Setter Property="Margin" Value="4"/>
        <Setter Property="Stretch" Value="Uniform"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Data" Value="M 0 0 L 5 5 L 10 0 Z"/>
        <Setter Property="Fill" Value="{DynamicResource FormText}"/>
    </Style>

所以,我有一个从按钮派生的自定义按钮对象.基本上它是一个显示箭头的小方形按钮.在我的自定义按钮的构造函数中,我有以下代码.

So, I have a custom buttom object that is derived from button. Basically it is a small square button that displays the arrow. In my constructor for my custom button I have the following code.

        Path Arrow = new Path();
        Arrow.Style = TryFindResource("ArrowStyle") as Style;

但是,对 TryFindResource 的调用失败表明它无法定位资源.如果我将定义的样式移动到 App.xaml 中,它会找到它,但当它是自定义控件资源时则不会.

However, the call to TryFindResource fails indicated that it cannot location the resource. If I move the defined style into App.xaml it find it, but not when it is a custom control resource.

我做错了什么或遗漏了什么?

What am I doing wrong or missing?

更新

我为其调用 TryFindResource 的自定义按钮确实驻留在自定义用户控件上.但是,由于我在自定义按钮的构造函数中调用 TryFindResource,因此在调用 TryFindResource 时,它​​实际上不会驻留在自定义用户控件上.创建自定义按钮后,它将被添加到自定义用户控件中.或许有不同的方式或地方可以放置 TryFindResource,以便在按钮属于控件之后调用它.

The custom button that I am calling TryFindResource for does reside on the custom user control. However, since I am calling TryFindResource in the constructor of the custom button it will not actually reside on the custom user control at the time TryFindResource is called. After the custom button is created it will be added to the custom user control. Perhaps there is a different way or place to put the TryFindResource so that it is called after the button belongs to the control.

推荐答案

TryFindResource 向上遍历逻辑树,到达父元素,直到到达根元素.

TryFindResource traverses the logical tree upward, to the parent element until the root element is reached.

然后检查应用程序资源.那是你第二次尝试的那个,因为它在那里被发现而奏效.

Then application resources are checked. That is the one you put in the second try, and worked because it was found there.

因此,当您说TryFindResource("ArrowStyle") 时,它会搜索其父元素上的箭头样式,直至其窗口资源,然后是应用程序资源(全局资源).当您从自定义按钮对象调用它时,它可以找不到,因为您的 ArrowStyle 资源不满足上述条件.

So when you say "TryFindResource("ArrowStyle"), it searches the arrow style on its parent elements up to its window resources and then application resources (global resources). When you call it from your custom button object, it can't find it because your ArrowStyle resource do not satisfy the conditions above.

您需要在从 UserControl 派生的自定义 WPF 控件"或其后代上调用它.

You need to call it on your "customized WPF control that is derived from UserControl" or descendants of it.

我认为如果你不能让它工作,只需将它移动到应用程序资源或将资源字典添加到具有所有全局资源(样式)的应用程序资源中:

I think if you can't make it work just move it to application resources or add a resource dictionary to your application resources with all global resources (styles):

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="....Your resources.xaml"/>
            <ResourceDictionary Source="....Your resources.xaml"/>
            <!-- Place further resources here -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这篇关于TryFindResource 无法找到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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