如何从 C# 代码访问 wpf 中的 ResourceDictionary?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/618648/
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
How can I access ResourceDictionary in wpf from C# code?
提问by Embedd_Khurja
I have a DataTemplatedefined in a xaml file that I want to access via C# code.
Can anyone please tell me how can I access it?
I added a new ResourceDictionaryfile and its name is Dictionary1.xaml.
I have a data template such as:
我DataTemplate在 xaml 文件中定义了一个我想通过 C# 代码访问的文件。谁能告诉我如何访问它?我添加了一个新ResourceDictionary文件,它的名字是Dictionary1.xaml。我有一个数据模板,例如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="mytemplate">
<TextBlock Text="Name:" Background="Blue"/>
</DataTemplate>
</ResourceDictionary>
not I have a ListBoxcalled listBox1 and I want to assign it to it's Itemtemplateproperty
but I'm not getting how can i do it?
不是我有一个ListBox叫做 listBox1 的,我想把它分配给它的Itemtemplate属性,但我不知道我该怎么做?
采纳答案by Szymon Rozga
Where exactly are you defining it?
你究竟在哪里定义它?
If you define it in the ResourceDictionary of your object, then
如果您在对象的 ResourceDictionary 中定义它,则
Application.Current.Resources[typeof(yourDataTemplateTargetType)]
should work. If you are defining it as a member of something else, like say, an ItemsControl, you need to get a handle to the ItemsControl instance and call the ItemTemplate property.
应该管用。如果您将其定义为其他对象的成员,例如 ItemsControl,则需要获取 ItemsControl 实例的句柄并调用 ItemTemplate 属性。
Edit: Ok, I think we're getting somewhere. So you are defining a ResourceDictionary in its own file. Before you can use it in your UI and access it from your code behind, you need to merge that ResourceDictionary into your application. Are you doing this?
编辑:好的,我想我们到了某个地方。所以你在它自己的文件中定义了一个 ResourceDictionary。在您可以在 UI 中使用它并从背后的代码访问它之前,您需要将该 ResourceDictionary 合并到您的应用程序中。你在做这个吗?
If you are, then the next step is to get this resource. Each FrameworkElement has a method called FindResource. This method is great because it walks up the ResourceDictionary tree and attempts to locate the resource with the key. So, if you want to access this resource from a UserControl, you can do the following in the code behind:
如果是,那么下一步就是获取此资源。每个 FrameworkElement 都有一个名为FindResource的方法。这个方法很棒,因为它沿着 ResourceDictionary 树向上走,并尝试使用键定位资源。因此,如果要从 UserControl 访问此资源,可以在后面的代码中执行以下操作:
FindResource(typeof(yourDataTemplateTargetType));
If this doesn't work for you, please show us exactly how you are declaring this resource dictionary and how it is getting merged into your application's resources.
如果这对您不起作用,请准确地向我们展示您如何声明此资源字典以及它如何合并到您的应用程序资源中。
回答by Jakob Christensen
If you for example have a template for Button in your resource dictionary in the App.xaml file you can access it using the following code:
例如,如果您在 App.xaml 文件的资源字典中有一个 Button 模板,则可以使用以下代码访问它:
Application.Current.Resources[typeof(Button)]
回答by itsho
Since Application.Currentwas null in my case, I've ended up using this:
由于Application.Current在我的情况下为空,我最终使用了这个:
var myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source =
new Uri("/DllName;component/Resources/MyResourceDictionary.xaml",
UriKind.RelativeOrAbsolute);
and then getting the specified key I needed by using
myResourceDictionary["KeyName"] as TypeOfItem
然后通过使用获取我需要的指定密钥
myResourceDictionary["KeyName"] as TypeOfItem
(source)
(来源)
回答by Alex Essilfie
You can access a resource dictionary you added to your project as follows:
您可以按如下方式访问您添加到项目中的资源字典:
var rd = new ResourceDictionary();
rd.Source = new Uri("ms-appx:///Dictionary1.xaml");
Then you can access a resource stored in the resource dictionary like so:
然后你可以像这样访问存储在资源字典中的资源:
someObject.Property = rd["mytemplate"];
NOTE:
You will have to modify the URI to the resource dictionary according to the location you created it relative to the project's base directory.
注意:
您必须根据相对于项目基本目录的创建位置来修改资源字典的 URI。
回答by dman
Any of the above approaches work getting the resource based on the location, if you are following MVVMm I would recommend doing it this way:
以上任何一种方法都可以根据位置获取资源,如果您正在关注 MVVMm,我建议您这样做:
- create a Service like ProvideDataTemplateService, (to create a service usual inherit from Behavior )
- Use Container of Your choice to inject this service where you would like to have aces to DataTemple.
- 创建一个像 ProvideDataTemplateService 这样的服务,(创建一个服务通常继承自 Behavior )
- 使用您选择的 Container 将此服务注入到您希望对 DataTemple 拥有 ace 的位置。
回答by fdsfdsfdsfds
I found the answer here
我在这里找到了答案
- create a ressource dictionary "ColorResources.xaml"
add to it: Blue
edit your app.xml and add:
use the color from your code
var color = (System.Windows.Media.Color)Application.Current.FindResource("ButtonColor1");
- 创建资源字典“ColorResources.xaml”
添加到它:蓝色
编辑您的 app.xml 并添加:
使用代码中的颜色
var color = (System.Windows.Media.Color)Application.Current.FindResource("ButtonColor1");
and voilà
瞧
ps : admin can you fix the code? it does not show up, thanks
ps:管理员可以修复代码吗?不显示,谢谢
回答by Bloggrammer
If you're getting the resources within the same project, try this:
如果您在同一个项目中获取资源,请尝试以下操作:
yourControl.Style = FindResource("YourResourceKey") as Style;
Otherwise, try this:
否则,试试这个:
ResourceDictionary res = (ResourceDictionary)Application.LoadComponent(new Uri("/ProjectName;component/FolderName/ResourceDictionaryName.xaml", UriKind.Relative));
yourControl.Style = (Style)res["YourResourceKey"];

