lang support for binding
This commit is contained in:
1 parent
95f35c7366
commit
1680858455
20 files changed
+274
-117
No files matched your search
@@ -8,6 +8,7 @@ using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Interactivity;
|
||||
using HandyControl.Properties.Langs;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
@@ -297,10 +298,9 @@ namespace HandyControl.Controls
|
||||
{
|
||||
if (panel == null) return;
|
||||
|
||||
var menuItem = new MenuItem
|
||||
{
|
||||
Header = Properties.Langs.Lang.Clear
|
||||
};
|
||||
var menuItem = new MenuItem();
|
||||
LangDecorator.SetLang(menuItem, HeaderedItemsControl.HeaderProperty, LangKeys.Clear);
|
||||
|
||||
menuItem.Click += (s, e) =>
|
||||
{
|
||||
foreach (var item in panel.Children.OfType<Growl>())
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using HandyControl.Tools;
|
||||
|
||||
namespace HandyControl.Properties.Langs
|
||||
@@ -10,7 +12,7 @@ namespace HandyControl.Properties.Langs
|
||||
|
||||
private static string CultureInfoStr;
|
||||
|
||||
public static CultureInfo Culture
|
||||
internal static CultureInfo Culture
|
||||
{
|
||||
get => Lang.Culture;
|
||||
set
|
||||
@@ -24,6 +26,13 @@ namespace HandyControl.Properties.Langs
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetLang(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string key) =>
|
||||
BindingOperations.SetBinding(dependencyObject, dependencyProperty, new Binding(key)
|
||||
{
|
||||
Source = Instance,
|
||||
Mode = BindingMode.OneWay
|
||||
});
|
||||
|
||||
private void UpdateLangs()
|
||||
{
|
||||
OnPropertyChanged(nameof(Am));
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#>
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using HandyControl.Tools;
|
||||
|
||||
namespace HandyControl.Properties.Langs
|
||||
@@ -29,7 +31,7 @@ namespace HandyControl.Properties.Langs
|
||||
|
||||
private static string CultureInfoStr;
|
||||
|
||||
public static CultureInfo Culture
|
||||
internal static CultureInfo Culture
|
||||
{
|
||||
get => Lang.Culture;
|
||||
set
|
||||
@@ -43,6 +45,13 @@ namespace HandyControl.Properties.Langs
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetLang(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string key) =>
|
||||
BindingOperations.SetBinding(dependencyObject, dependencyProperty, new Binding(key)
|
||||
{
|
||||
Source = Instance,
|
||||
Mode = BindingMode.OneWay
|
||||
});
|
||||
|
||||
private void UpdateLangs()
|
||||
{
|
||||
<#foreach(var item in propertyNameList){#>
|
||||
|
||||
@@ -1,42 +1,163 @@
|
||||
using HandyControl.Properties.Langs;
|
||||
using System;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
using HandyControl.Properties.Langs;
|
||||
|
||||
namespace HandyControl.Tools.Extension
|
||||
{
|
||||
public class LangExtension : MarkupExtension
|
||||
{
|
||||
private readonly DependencyObject _proxy;
|
||||
|
||||
private bool _isInternalAction;
|
||||
|
||||
public LangExtension()
|
||||
{
|
||||
|
||||
_proxy = new DependencyObject();
|
||||
Source = LangDecorator.Instance;
|
||||
}
|
||||
|
||||
public LangExtension(string key) => Key = key;
|
||||
public LangExtension(string key) : this()
|
||||
{
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty KeyProperty = DependencyProperty.RegisterAttached(
|
||||
"Key", typeof(object), typeof(LangExtension), new PropertyMetadata(default));
|
||||
|
||||
public object Key
|
||||
{
|
||||
get => _proxy.GetValue(KeyProperty);
|
||||
set => _proxy.SetValue(KeyProperty, value);
|
||||
}
|
||||
|
||||
private static readonly DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached(
|
||||
"TargetProperty", typeof(DependencyProperty), typeof(LangExtension), new PropertyMetadata(default(DependencyProperty)));
|
||||
|
||||
private static void SetTargetProperty(DependencyObject element, DependencyProperty value)
|
||||
=> element.SetValue(TargetPropertyProperty, value);
|
||||
|
||||
private static DependencyProperty GetTargetProperty(DependencyObject element)
|
||||
=> (DependencyProperty) element.GetValue(TargetPropertyProperty);
|
||||
|
||||
public BindingMode Mode { get; set; }
|
||||
|
||||
public IValueConverter Converter { get; set; }
|
||||
|
||||
public object ConverterParameter { get; set; }
|
||||
|
||||
public object Source { get; set; }
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (!(serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget pvt))
|
||||
return null;
|
||||
if (_isInternalAction) return this;
|
||||
if (!(serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget provideValueTarget)) return this;
|
||||
if (provideValueTarget.TargetObject.GetType().FullName == "System.Windows.SharedDp") return this;
|
||||
if (!(provideValueTarget.TargetObject is DependencyObject targetObject)) return this;
|
||||
if (!(provideValueTarget.TargetProperty is DependencyProperty targetProperty)) return this;
|
||||
|
||||
if (!(pvt.TargetObject is DependencyObject targetObject))
|
||||
return null;
|
||||
|
||||
if (!(pvt.TargetProperty is DependencyProperty targetProperty))
|
||||
return null;
|
||||
|
||||
var binding = new Binding(Key)
|
||||
switch (Key)
|
||||
{
|
||||
Source = LangSource,
|
||||
Mode = BindingMode.OneWay
|
||||
};
|
||||
BindingOperations.SetBinding(targetObject, targetProperty, binding);
|
||||
return binding.ProvideValue(serviceProvider);
|
||||
case string key:
|
||||
{
|
||||
var binding = new Binding(key)
|
||||
{
|
||||
Converter = Converter,
|
||||
ConverterParameter = ConverterParameter,
|
||||
UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
|
||||
Source = Source,
|
||||
Mode = BindingMode.OneWay
|
||||
};
|
||||
BindingOperations.SetBinding(targetObject, targetProperty, binding);
|
||||
return binding.ProvideValue(serviceProvider);
|
||||
}
|
||||
case Binding keyBinding when targetObject is FrameworkElement element:
|
||||
{
|
||||
if (element.DataContext != null)
|
||||
{
|
||||
_isInternalAction = true;
|
||||
SetLangBinding(element, targetProperty, keyBinding.Path, element.DataContext);
|
||||
_isInternalAction = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetTargetProperty(element, targetProperty);
|
||||
element.DataContextChanged += LangExtension_DataContextChanged;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case Binding keyBinding when targetObject is FrameworkContentElement element:
|
||||
{
|
||||
if (element.DataContext != null)
|
||||
{
|
||||
_isInternalAction = true;
|
||||
SetLangBinding(element, targetProperty, keyBinding.Path, element.DataContext);
|
||||
_isInternalAction = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetTargetProperty(element, targetProperty);
|
||||
element.DataContextChanged += LangExtension_DataContextChanged;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string Key { get; set; }
|
||||
private void LangExtension_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
switch (sender)
|
||||
{
|
||||
case FrameworkElement element:
|
||||
{
|
||||
element.DataContextChanged -= LangExtension_DataContextChanged;
|
||||
if (!(Key is Binding keyBinding)) return;
|
||||
|
||||
public virtual object LangSource => LangDecorator.Instance;
|
||||
var targetProperty = GetTargetProperty(element);
|
||||
SetTargetProperty(element, null);
|
||||
SetLangBinding(element, targetProperty, keyBinding.Path, element.DataContext);
|
||||
break;
|
||||
}
|
||||
case FrameworkContentElement element:
|
||||
{
|
||||
element.DataContextChanged -= LangExtension_DataContextChanged;
|
||||
if (!(Key is Binding keyBinding)) return;
|
||||
|
||||
var targetProperty = GetTargetProperty(element);
|
||||
SetTargetProperty(element, null);
|
||||
SetLangBinding(element, targetProperty, keyBinding.Path, element.DataContext);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetLangBinding(DependencyObject targetObject, DependencyProperty targetProperty, PropertyPath path, object dataContext)
|
||||
{
|
||||
if (targetProperty == null) return;
|
||||
|
||||
BindingOperations.SetBinding(targetObject, targetProperty, new Binding
|
||||
{
|
||||
Path = path,
|
||||
Source = dataContext,
|
||||
Mode = BindingMode.OneWay
|
||||
});
|
||||
|
||||
var key = targetObject.GetValue(targetProperty) as string;
|
||||
if (string.IsNullOrEmpty(key)) return;
|
||||
|
||||
BindingOperations.SetBinding(targetObject, targetProperty, new Binding(key)
|
||||
{
|
||||
Converter = Converter,
|
||||
ConverterParameter = ConverterParameter,
|
||||
UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
|
||||
Source = Source,
|
||||
Mode = BindingMode.OneWay
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using System.Windows.Media.Animation;
|
||||
using System.Windows.Navigation;
|
||||
using HandyControl.Controls;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Properties.Langs;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
@@ -37,13 +38,11 @@ namespace HandyControl.Tools
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSystemVersionInfo(SystemVersionInfo info)
|
||||
{
|
||||
BlurWindow.SystemVersionInfo = info;
|
||||
}
|
||||
public void SetSystemVersionInfo(SystemVersionInfo info) => BlurWindow.SystemVersionInfo = info;
|
||||
|
||||
public void SetLang(string lang)
|
||||
{
|
||||
LangDecorator.Culture = new CultureInfo(lang);
|
||||
Application.Current.Dispatcher.Thread.CurrentUICulture = new CultureInfo(lang);
|
||||
Lang = XmlLanguage.GetLanguage(lang);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user