从WPF用户控件访问的ResourceDictionary [英] Accessing ResourceDictionary from WPF UserControl

查看:1225
本文介绍了从WPF用户控件访问的ResourceDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问一个资源字典在用户控件代码隐藏通过C#和我有一点成功

I'm trying to access a resource dictionary in a UserControl code-behind via C# and I'm having little success.

合并解释:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Resources/BiometricDictionary.xaml" />                
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>



嵌入式词典:

Embedded Dictionary:

<UserControl.Resources>
    <BitmapImage x:Key="imageDefault">/Resources/Images/default_thumb.png</BitmapImage>
    <BitmapImage x:Key="imageDisconnected">/Resources/Images/disconnect_thumb.png</BitmapImage>
    <BitmapImage x:Key="imageFailed">/Resources/Images/failed_thumb.png</BitmapImage>
    <BitmapImage x:Key="imageSuccess">/Resources/Images/success_thumb.png</BitmapImage>
</UserControl.Resources>



后面的代码:

Code behind:

        var resourceDictionary = new ResourceDictionary();
        resourceDictionary.Source = new Uri("/Resources/BiometricDictionary.xaml", UriKind.Relative);



我已经尝试了所有的例子和有用的提示,但来了短。眼下,成功将载入字典的能力。有什么建议么?

I've tried all of the examples and helpful tips but coming up short. Right now, success would be the ability to load the dictionary. Any suggestions?

推荐答案

要访问您的UserControl的XAML的资源之一,你的代码隐藏,所有你需要做的是访问用户控件的资源属性。事情是这样的:

To access one of your UserControl's XAML resources in your codebehind, all you need to do is access the Resources property of the UserControl. Something like this:

BitmapImage myImage = (BitmapImage)this.Resources["imageDefault"];



虽然,优选的方法是使用FindResource(),这将搜索整个逻辑树为一比赛的关键,而不仅仅是它的对象上调用。

Though, the preferred method is to use FindResource(), which will search the entire logical tree for a match to the key, rather than just the object it is called on.

BitmapImage myImage = (BitmapImage)this.FindResource("imageDefault");

这篇关于从WPF用户控件访问的ResourceDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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