2017-05-15 27 views
1

ViewModelで宣言されている["SampleName"]というプロパティをバインドしようとしています。 SampleNameはキーです。しかし、コードの下にしようとすると、表示されません。WPFローカライズエクステンションMVVMバインディング

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

お願いします。

+0

私は同じ問題を抱えています。解決策は見つかりましたか? –

答えて

0

私はソースコードをチェックしましたが、BLocがそれをしたとは思わないのです。しかし、私は同じ結果をアーカイブする別の方法を見つけました。

thisの回答に基づいて、私はこの解決策を考え出しました。ビューにリソースキーを与える代わりに、リソース値を与えます。カルチャーが変更された場合、値も変更されます。これはコア実装です。

public class MyViewModel : INotifyPropertyChanged { 

    public MyViewModel() 
    { 
     // Properties.Resources is my Resources.resx file and String1 is my resource key 
     BindPropertyToResource(nameof(Name), nameof(Properties.Resources.String1)); 
    } 


    private string _name; 
    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (_name != value) 
      { 
       _name = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    protected void BindPropertyToResource(string propertyName, string resourceKey){ 
     WPFLocalizeExtension.Providers.ResxLocalizationProvider resxLocalizationProvider 
      = WPFLocalizeExtension.Providers.ResxLocalizationProvider.Instance; 

     WPFLocalizeExtension.Providers.ResxLocalizationProvider.SetDefaultAssembly(resxLocalizationProvider, System.Reflection.Assembly.GetCallingAssembly().GetName().Name); 
     WPFLocalizeExtension.Providers.ResxLocalizationProvider.SetDefaultDictionary(resxLocalizationProvider, nameof(Properties.Resources)); 

     var targetProperty = this.GetType().GetProperty(propertyName); 
     var locBinding = new WPFLocalizeExtension.Extensions.LocTextExtension(resourceKey); 

     locBinding.SetBinding(this, targetProperty); 
    } 


    #region INotifyPropertyChanged Members 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion // INotifyPropertyChanged Members 
} 

この回避策が問題を解決することを願っています。