static void CallbackOptions(this DependencyObjectProperty objectProperty, LocalizationCallback callback, string stringFormat = null, string resourceKey = null) { Localize(objectProperty, new LocalizationOptions() { Callback = callback, StringFormat = stringFormat, Key = resourceKey }); }
static void BindingOptions(this DependencyObjectProperty objectProperty, BindingBase binding, ICollection <BindingBase> bindings, string stringFormat = null, string resourceKey = null) { Localize(objectProperty, new LocalizationOptions() { Binding = binding, Bindings = bindings, StringFormat = stringFormat, Key = resourceKey }); }
/// <summary> /// Localizes the specified objectProperty with a resource value. /// </summary> /// <param name="objectProperty"></param> /// <param name="key"></param> public static void Resource(this DependencyObjectProperty objectProperty, string key, IValueConverter converter) { Localize(objectProperty, new LocalizationOptions() { Key = key, Converter = converter, }); }
/// <summary> /// Creates a new localized value. /// </summary> /// <param name="targetObject"></param> /// <param name="targetProperty"></param> /// <param name="binding"></param> /// <param name="bindings"></param> /// <returns></returns> public static LocalizedValue Create( DependencyObjectProperty objectProperty, LocalizationOptions options ) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var localizedValue = new LocalizedValue(objectProperty.TargetObject, objectProperty.TargetProperty) { Key = options.Key, StringFormat = options.StringFormat, Callback = options.Callback, CallbackParameter = options.CallbackParameter, Converter = options.Converter, ConverterParameter = options.ConverterParameter, }; if (options.Binding != null || options.Bindings?.Count > 0) { if (false == (objectProperty.TargetProperty.PropertyObject is DependencyProperty)) { // The what bindings are implemented in WPF provides no way to obtain the value // produced by the binging. The only way is to update the property directly. Therefore, // the extension cannot support bindings on non-dependency properties (same as WPF). throw new ArgumentException("Bindings are supported only on dependency properties.", nameof(options)); } // Create a binding var localizedBinding = new MultiBinding() { Mode = BindingMode.OneWay, }; if (options.Binding != null) { localizedBinding.Bindings.Add(options.Binding); } if (options.Bindings?.Count > 0) { foreach (var item in options.Bindings) { localizedBinding.Bindings.Add(item); } } localizedBinding.Converter = localizedValue; localizedValue.BindingExpression = (BindingExpressionBase)localizedBinding.ProvideValue(objectProperty); } return(localizedValue); }
/// <summary> /// Localizes the specified objectProperty. /// </summary> /// <param name="objectProperty">The objectProperty to localize.</param> /// <param name="options">The options describing how the objectProperty is to be localized.</param> /// <exception cref="ArgumentException">A binding is specified and the objectProperty is a non-dependency objectProperty.</exception> public static void Localize(this DependencyObjectProperty objectProperty, LocalizationOptions options) { var localizedValue = LocalizedValue.Create(objectProperty, options); LocalizationManager.Add(localizedValue); // Set the value initially if (localizedValue.BindingExpression != null) { // The value uses bindings localizedValue.TargetProperty.SetValue(localizedValue.TargetObject, localizedValue.BindingExpression); } else { // The value does not use bindings localizedValue.TargetProperty.SetValue(localizedValue.TargetObject, localizedValue.ProduceValue()); } }
/// <summary> /// Removes the localization from the specified objectProperty. /// </summary> /// <param name="objectProperty"></param> /// <remarks> /// This method stops localizing the specified value (the value will no longer be updated /// when the culture changes). However, this method does not remove the value that is already set. If there /// was a localized binding and the some of the data bound values change the objectProperty will still be updated. /// </remarks> public static void Remove(this DependencyObjectProperty objectProperty) { LocalizationManager.RemoveProperty(objectProperty.TargetObject, objectProperty.TargetProperty); }
/// <summary> /// Localizes the specified objectProperty by using the specified binding to obtain a value and then formats the value using the specified /// string resource. /// </summary> /// <param name="objectProperty"></param> public static void BindingResourceFormat(this DependencyObjectProperty objectProperty, string resourceKey, params BindingBase[] bindings) { BindingOptions(objectProperty, null, bindings, resourceKey: resourceKey); }
public static void BindingFormat(this DependencyObjectProperty objectProperty, string stringFormat, params BindingBase[] bindings) { BindingOptions(objectProperty, null, bindings, stringFormat: stringFormat); }
/// <summary> /// Localizes the specified objectProperty by using the specified binding to obtain a value and then formats the value using the specified /// string resource. /// </summary> /// <param name="objectProperty"></param> public static void BindingResourceFormat(this DependencyObjectProperty objectProperty, ICollection <BindingBase> bindings, string resourceKey) { BindingOptions(objectProperty, null, bindings, resourceKey: resourceKey); }
public static void BindingFormat(this DependencyObjectProperty objectProperty, ICollection <BindingBase> bindings, string stringFormat) { BindingOptions(objectProperty, null, bindings, stringFormat: stringFormat); }
/// <summary> /// Localizes the specified objectProperty by using the specified binding to obtain a value and then formats the value using the specified /// string resource. /// </summary> /// <param name="objectProperty"></param> public static void BindingResourceFormat(this DependencyObjectProperty objectProperty, BindingBase binding, string resourceKey) { BindingOptions(objectProperty, binding, null, resourceKey: resourceKey); }
public static void BindingFormat(this DependencyObjectProperty objectProperty, BindingBase binding, string stringFormat) { BindingOptions(objectProperty, binding, null, stringFormat: stringFormat); }
/// <summary> /// Localizes the specified objectProperty by using the specified callback to obtain a value and then formats the value using the specified /// string resource. /// </summary> /// <param name="objectProperty"></param> public static void CallbackResourceFormat(this DependencyObjectProperty objectProperty, LocalizationCallback callback, string resourceKey) { CallbackOptions(objectProperty, callback, resourceKey: resourceKey); }
public static void CallbackFormat(this DependencyObjectProperty objectProperty, LocalizationCallback callback, string stringFormat) { CallbackOptions(objectProperty, callback, stringFormat: stringFormat); }
public static void Callback(this DependencyObjectProperty objectProperty, LocalizationCallback callback) { CallbackOptions(objectProperty, callback); }
/// <summary> /// Localizes the specified objectProperty with a resource value. /// </summary> /// <param name="objectProperty"></param> /// <param name="key"></param> public static void Resource(this DependencyObjectProperty objectProperty, string key) { Resource(objectProperty, key, null); }