Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Il y a quelques temps déjà, j’ai ajouté à slextensions une classe permettant de localiser simplement une application à partir d’attachedproperty. La mode étant aux behaviors, j’ai rajouté une classe permettant cette localisation à la branche SL3 de slextensions. Cette classe possède 4 propriétés :
L’utilisation est très simple voici un exemple de localisation d’un texte
<TextBlock >
<i:Interaction.Behaviors>
<SLExtensions_Interactivity:Localize Key="WelcomeMessage" PropertyName="Text" ResourceManagerKey="MyResource" />
</i:Interaction.Behaviors>
</TextBlock>
et d’un ContentControl
<ContentControl Height="121" Width="244" Canvas.Left="25" Canvas.Top="206" >
<SLExtensions_Interactivity:Localize Key="Welcome" PropertyName="Content" ResourceManagerKey="MyResource" ConvertXaml="True"/>
</ContentControl>
Le code de la classe :
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity;
using System.Resources;
using System.Globalization;
using System.Windows.Markup;
using SLExtensions.Globalization;
namespace SLExtensions.Interactivity
{
public class Localize : Behavior<FrameworkElement>
private string key;
public string Key
get
return this.key;
}
set
if (this.key != value)
this.key = value;
Refresh();
private bool convertXaml;
public bool ConvertXaml
return this.convertXaml;
if (this.convertXaml != value)
this.convertXaml = value;
private DependencyProperty dependencyProperty;
private string propertyName;
public string PropertyName
return this.propertyName;
if (this.propertyName != value)
this.propertyName = value;
this.dependencyProperty = null;
private string resourceManagerKey;
public string ResourceManagerKey
return this.resourceManagerKey;
if (this.resourceManagerKey != value)
this.resourceManagerKey = value;
private void Refresh()
if (AssociatedObject == null
|| string.IsNullOrEmpty(propertyName)
|| string.IsNullOrEmpty(Key)
|| string.IsNullOrEmpty(ResourceManagerKey))
return;
if (dependencyProperty == null)
Type t = AssociatedObject.GetType();
var field = t.GetField(propertyName + "Property", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
if (field != null)
dependencyProperty = field.GetValue(null) as DependencyProperty;
if (dependencyProperty != null
&& Application.Current != null)
ResourceManager resourceManager = null;
var obj = Application.Current.Resources[ResourceManagerKey];
if (obj is ResourceLoader)
resourceManager = ((ResourceLoader)obj).GetResourceManager();
else
resourceManager = obj as ResourceManager;
var resourceValue = resourceManager.GetObject(Key, CultureInfo.CurrentCulture);
if (convertXaml && resourceValue is string)
resourceValue = XamlReader.Load((string)resourceValue);
AssociatedObject.SetValue(dependencyProperty, resourceValue);
protected override void OnAttached()
base.OnAttached();
protected override void OnDetaching()
base.OnDetaching();