Created
May 3, 2015 15:56
-
-
Save dfyx/b5f3e03e280f02ac8185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Globalization; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Markup; | |
using Cirrious.CrossCore.Wpf.Converters; | |
using Cirrious.MvvmCross.Localization; | |
namespace MyNamespace | |
{ | |
public class LangBinding : MarkupExtension | |
{ | |
private Binding _binding = new Binding(); | |
public LangBinding() | |
{ | |
Mode = BindingMode.OneWay; | |
} | |
public LangBinding(PropertyPath path) | |
{ | |
Mode = BindingMode.OneWay; | |
Path = path; | |
} | |
public string Key { get; set; } | |
public override object ProvideValue(IServiceProvider serviceProvider) | |
{ | |
var translationBinding = new Binding {Path = new PropertyPath("TextSource")}; | |
if (Path == null) | |
{ | |
translationBinding.Converter = new MvxNativeValueConverter<MvxLanguageConverter>(); | |
translationBinding.ConverterParameter = Key; | |
return translationBinding.ProvideValue(serviceProvider); | |
} | |
var multiBinding = new MultiBinding(); | |
multiBinding.Bindings.Add(translationBinding); | |
multiBinding.Bindings.Add(Binding); | |
multiBinding.Converter = new MultiLanguageConverter(); | |
return multiBinding.ProvideValue(serviceProvider); | |
} | |
#region properties | |
/// <summary> | |
/// The decorated binding class. | |
/// </summary> | |
[Browsable(false)] | |
public Binding Binding | |
{ | |
get { return _binding; } | |
set { _binding = value; } | |
} | |
[DefaultValue(null)] | |
public object AsyncState | |
{ | |
get { return _binding.AsyncState; } | |
set { _binding.AsyncState = value; } | |
} | |
[DefaultValue(false)] | |
public bool BindsDirectlyToSource | |
{ | |
get { return _binding.BindsDirectlyToSource; } | |
set { _binding.BindsDirectlyToSource = value; } | |
} | |
[DefaultValue(null)] | |
public IValueConverter Converter | |
{ | |
get { return _binding.Converter; } | |
set { _binding.Converter = value; } | |
} | |
[DefaultValue(null)] | |
public object TargetNullValue | |
{ | |
get { return _binding.TargetNullValue; } | |
set { _binding.TargetNullValue = value; } | |
} | |
[TypeConverter(typeof (CultureInfoIetfLanguageTagConverter)), DefaultValue(null)] | |
public CultureInfo ConverterCulture | |
{ | |
get { return _binding.ConverterCulture; } | |
set { _binding.ConverterCulture = value; } | |
} | |
[DefaultValue(null)] | |
public object ConverterParameter | |
{ | |
get { return _binding.ConverterParameter; } | |
set { _binding.ConverterParameter = value; } | |
} | |
[DefaultValue(null)] | |
public string ElementName | |
{ | |
get { return _binding.ElementName; } | |
set { _binding.ElementName = value; } | |
} | |
[DefaultValue(null)] | |
public object FallbackValue | |
{ | |
get { return _binding.FallbackValue; } | |
set { _binding.FallbackValue = value; } | |
} | |
[DefaultValue(false)] | |
public bool IsAsync | |
{ | |
get { return _binding.IsAsync; } | |
set { _binding.IsAsync = value; } | |
} | |
[DefaultValue(BindingMode.Default)] | |
public BindingMode Mode | |
{ | |
get { return _binding.Mode; } | |
set { _binding.Mode = value; } | |
} | |
[DefaultValue(false)] | |
public bool NotifyOnSourceUpdated | |
{ | |
get { return _binding.NotifyOnSourceUpdated; } | |
set { _binding.NotifyOnSourceUpdated = value; } | |
} | |
[DefaultValue(false)] | |
public bool NotifyOnTargetUpdated | |
{ | |
get { return _binding.NotifyOnTargetUpdated; } | |
set { _binding.NotifyOnTargetUpdated = value; } | |
} | |
[DefaultValue(false)] | |
public bool NotifyOnValidationError | |
{ | |
get { return _binding.NotifyOnValidationError; } | |
set { _binding.NotifyOnValidationError = value; } | |
} | |
[DefaultValue(null)] | |
public PropertyPath Path | |
{ | |
get { return _binding.Path; } | |
set { _binding.Path = value; } | |
} | |
[DefaultValue(null)] | |
public RelativeSource RelativeSource | |
{ | |
get { return _binding.RelativeSource; } | |
set { _binding.RelativeSource = value; } | |
} | |
[DefaultValue(null)] | |
public object Source | |
{ | |
get { return _binding.Source; } | |
set { _binding.Source = value; } | |
} | |
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] | |
public UpdateSourceExceptionFilterCallback UpdateSourceExceptionFilter | |
{ | |
get { return _binding.UpdateSourceExceptionFilter; } | |
set { _binding.UpdateSourceExceptionFilter = value; } | |
} | |
[DefaultValue(UpdateSourceTrigger.Default)] | |
public UpdateSourceTrigger UpdateSourceTrigger | |
{ | |
get { return _binding.UpdateSourceTrigger; } | |
set { _binding.UpdateSourceTrigger = value; } | |
} | |
[DefaultValue(false)] | |
public bool ValidatesOnDataErrors | |
{ | |
get { return _binding.ValidatesOnDataErrors; } | |
set { _binding.ValidatesOnDataErrors = value; } | |
} | |
[DefaultValue(false)] | |
public bool ValidatesOnExceptions | |
{ | |
get { return _binding.ValidatesOnExceptions; } | |
set { _binding.ValidatesOnExceptions = value; } | |
} | |
[DefaultValue(null)] | |
public string XPath | |
{ | |
get { return _binding.XPath; } | |
set { _binding.XPath = value; } | |
} | |
[DefaultValue(null)] | |
public Collection<ValidationRule> ValidationRules | |
{ | |
get { return _binding.ValidationRules; } | |
} | |
[DefaultValue(null)] | |
public string StringFormat | |
{ | |
get { return _binding.StringFormat; } | |
set { _binding.StringFormat = value; } | |
} | |
[DefaultValue("")] | |
public string BindingGroupName | |
{ | |
get { return _binding.BindingGroupName; } | |
set { _binding.BindingGroupName = value; } | |
} | |
#endregion | |
} | |
public class MultiLanguageConverter : IMultiValueConverter | |
{ | |
private readonly MvxLanguageConverter _languageConverter = new MvxLanguageConverter(); | |
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) | |
{ | |
return _languageConverter.Convert(values[0], targetType, values[1], culture); | |
} | |
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) | |
{ | |
throw new NotSupportedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
<Label Content="{mvx:LangBinding Key=String1}" />
for a static key<Label Content="{mvx:LangBinding Text}" />
whereText
itself is a ViewModel property