WPF 中类的默认 ValueConverter
我正要开始一个新项目,目前正在评估一些本地化、模块化等技术。
我(至少在我看来)有一个很好的本地化方法,但现在我很难找到一个好的数据绑定解决方案。
我想将文本字段等(通常是 UIElements)的值绑定到表单的 DataContext 中的特定方法。 该方法如下所示:
public void GetValue(string name)
其中 name 是“节点/子节点/子子节点”形式的“路径”。 我考虑过使用 ValueConverter 进行绑定,到目前为止效果很好。
我的绑定表达式如下所示:
{Binding Path=Localization, Converter={StaticResource LocalizationConverter}, ConverterParameter=PrismBreak/Shell.xaml/New}
我认为这个绑定表达式虽然完成了它的工作,但有点失败最多可用于每个 UIElement。 所以我想是否可以为某种类型定义一个默认的 ValueConverter(--> 每当该类型绑定到使用 ValueConverter 的地方时)。
这在 WPF 中可能吗?
如果不可能,还有其他好的方法可以绑定到具有动态参数的方法吗?
预先致谢并致以最诚挚的问候,
克里斯
I'm just about to start a new project of mine and am currently evaluating some techniques for localization, modularity etc.
I have (at least in my opinion) a pretty good approach to localization but now I struggle to find a good solution for databinding.
I want to bind values of textfields etc. (UIElements in general) to a specific method in the DataContext of the form. The method looks like this:
public void GetValue(string name)
where name is a "path" in the form of "node/subnode/subsubnode". I thought about using a ValueConverter for the binding and this worked out pretty good till now.
My binding expression looks like that:
{Binding Path=Localization, Converter={StaticResource LocalizationConverter}, ConverterParameter=PrismBreak/Shell.xaml/New}
I think that this binding expression although doing its job is kinda blown up to use for every single UIElement. So I thought if it was possible to define a default ValueConverter for a certain type (--> whenever that type is bound to somewhere that ValueConverter is used).
Is that possible in WPF?
If it's not possible is there any other good way to bind to a method with dynamic parameters?
Thanks in advance and best regards,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 WPF 进行本地化的规定方法是在附属程序集中创建本地化的 BAML; 这是通过将 x:Uid 附加属性添加到您的元素来实现的希望本地化。 MSDN 上有一个很好的 WPF 全球化和本地化n 部分,描述了技术。
我建议的另一种方法是创建静态“本地化字典”类型,并实现 ICustomTypeDescriptor,将传入的属性绑定重定向到字典中的字符串。 您最终可能会做这样的事情:
并且您可以继续利用现有技术,例如 RESX + Satellite 组件。
The prescribed way of doing localization for WPF, is to create localized BAML in satellite assemblies; this is achieved by adding the x:Uid attached property to elements you wished to be localized. There is a nice WPF Globalization and Localization section on MSDN describing the techniques.
Another approach I would suggest would be to create a static 'Localization Dictionary' type, and to implement ICustomTypeDescriptor, to redirect incoming property binds to strings in your dictionary. You could end up doing something like this:
And you could continue to leverage existing technologies such as RESX + Satellite assemblies.