WPF本地化扩展MVVM绑定 [英] WPF Localized Extension MVVM Binding
问题描述
我试图绑定在ViewModel中声明的名为["SampleName"]的属性.这里的SampleName是关键.但是,当尝试下面的代码时,它显示为空.
I am trying to bind a property called ["SampleName"] that is declared in ViewModel. Here SampleName is key. However, when trying below code, it display's empty.
<TextBlock FontSize="14" Text="{lex:BLoc Value=SampleName}" lex:ResxLocalizationProvider.DefaultAssembly="SAS.Resources" lex:ResxLocalizationProvider.DefaultDictionary="Report" HorizontalAlignment="Center" VerticalAlignment="Top" TextDecorations="Underline" FontWeight="DemiBold" Grid.Row="0" Grid.ColumnSpan="{Binding Path=SpanCount}" Grid.Column="0"/>
请建议我.
推荐答案
我使用的是正确答案,但这并不是100%稳定且线程安全的.现在我改用这种解决方案.
I was using the sugested answer, but that's not 100% stable and thread safe. Now I am using this solution instead.
我的观点:
xmlns:lex="http://wpflocalizeextension.codeplex.com"
<TextBlock Text="{Binding ElementName=LocProxy, Path=Result}"/>
<lex:LocProxy Name="LocProxy"
Source="{Binding NameOfResourceKey}"/>
MyViewModel:
MyViewModel:
public string NameOfResourceKey { get; set; }
,然后将其设置为int ViewModel:
and then set it like this int ViewModel:
// Use your way to access classis Resources.resx file
NameOfResourceKey = nameof(Properties.Resources.YourLocalizedMessageKey);
// Use your function to RaiseProperty
RaisePropertyChanged(nameof(NameOfResourceKey));
因此,您需要通过nameof引用资源文件中的Resource,将其设置为lex:LocProxy的源,然后在需要本地化字符串的任何位置绑定到LoxProxy Result.非常简单,然后将所有本地化都交给GUI,这是最佳的.
So you need to reference your Resource in resource file by nameof, set it as a source of lex:LocProxy and then Bind to LoxProxy Result, wherever you need your localized string. Quite simple, and then GUI is handlig all the localization, which is optimal.
要提高属性,可以使用bruno.almeida(OnPropertyChanged)中的函数,也可以使用MVVMLight nuget包示例,并像我一样在ViewModel中继承ViewModelBase.
And for Raise the property, you can use the function from bruno.almeida (OnPropertyChanged), or use for Example MVVMLight nuget package and inherit ViewModelBase in your ViewModel as I do.
这篇关于WPF本地化扩展MVVM绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!