XAML中的数据绑定资源文件 [英] Databind Resource File in XAML
问题描述
对于本地化,我正在使用.NET中的资源文件(.resx文件)功能,但是我想知道是否存在一种聪明的方法来直接在XAML中对各种本地化属性进行数据绑定?
For localization I'm using the Resource-file (.resx files) functionality in .NET, but I'm wondering if there's a smart way to databind the various localization properties directly in XAML?
资源文件似乎只公开了静态属性,我无法弄清楚如何从视图模型或其他资源字典中进行绑定.
The resource file only seems to expose static properties, which I can't figure out how to bind from a viewmodel, or other resource dictionary.
此外,如果可能的话,我希望它可以在设计时与Expression Blend一起使用.
Also, if it's possible, I'd like it to work at design-time with Expression Blend.
推荐答案
这是我的方法.
WPF:
-
创建一个资源文件,并在同一程序集中创建一个具有公共构造函数的类.确保资源文件被标记为公共.
Create a resource file and in the same assembly create a class that has a public constructor. Make sure the resource file is marked public.
在您的xaml文件中-在名称空间中添加对此位置的引用
In your xaml file - add a reference to this location in the namespaces
xmlns:res ="clr-namespace:MyProject.StringResources"
对于文本属性,请使用以下绑定
For your text property use the following binding
TextProperty ="{x:静态res:ResourceFileName.ResourceKey}"
Silverlight:
Silverlight:
-
按照步骤1&2,然后将资源文件作为资源添加到用户控件或应用程序级资源中:
Follow steps 1 & 2 above and then add the resource file as a Resource in either your user control or in an application level resource:
< res:ResourceFileName x:Key ="resourcesLabels"/>
对于文本属性,请使用以下绑定:
For your text property use the following binding:
TextProperty ="{Binding ResourceKey,Source = {StaticResource resourceLabels}}"
这篇关于XAML中的数据绑定资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!