Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Localized Extension MVVM Binding

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"/>

Please suggest me.

like image 958
Nitheen Rao Avatar asked Jan 19 '26 13:01

Nitheen Rao


1 Answers

I was using the sugested answer, but that's not 100% stable and thread safe. Now I am using this solution instead.

My View:

xmlns:lex="http://wpflocalizeextension.codeplex.com"

<TextBlock Text="{Binding ElementName=LocProxy, Path=Result}"/>
<lex:LocProxy Name="LocProxy"
           Source="{Binding NameOfResourceKey}"/>

MyViewModel:

public string NameOfResourceKey { get; set; }

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)); 

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.

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.

like image 137
Marek Mikulec Avatar answered Jan 21 '26 08:01

Marek Mikulec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!