wpf 通过名称访问c#代码资源字典的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14843506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:29:52  来源:igfitidea点击:

Access to the elements of a resource dictionary of c# code by name

c#wpfdictionaryresourcedictionary

提问by Mixer

How I can code in c# to get access to the Image my_image_namein the resource? And as I gave resource dictionary name mydic_name. Can I refer to the dictionary by name?

如何在 c# 中编码以访问资源中的 Image my_image_name?正如我给资源字典命名mydic_name。我可以按名字查字典吗?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="mydic_name">
                    <Image x:Name="my_image_name" x:Key="my_image_key" Source="Properties/images/device1.png"/>
</ResourceDictionary>

回答by sa_ddam213

Assuming your ResourceDictionaryis loaded yo can do something like this:

假设您ResourceDictionary已加载,您可以执行以下操作:

 var image = (Image)Application.Current.Resources.FindName("my_image_name");