lang support for binding

This commit is contained in:
NaBian 2020-04-18 16:42:02 +08:00
parent 95f35c7366
commit 1680858455
20 changed files with 274 additions and 117 deletions

View File

@ -1,5 +1,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using HandyControl.Tools;
namespace HandyControlDemo.Properties.Langs
@ -24,6 +26,13 @@ namespace HandyControlDemo.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(About));

View File

@ -19,6 +19,8 @@
#>
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using HandyControl.Tools;
namespace HandyControlDemo.Properties.Langs
@ -43,6 +45,13 @@ namespace HandyControlDemo.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){#>

View File

@ -8,6 +8,7 @@ using System.Net;
using System.Windows;
using HandyControl.Data;
using HandyControlDemo.Data;
using HandyControlDemo.Properties.Langs;
using HandyControlDemo.Tools.Converter;
using Newtonsoft.Json;
@ -106,7 +107,7 @@ namespace HandyControlDemo.Service
var list = new List<string>();
for (var i = 1; i <= 9; i++)
{
list.Add($"{converter.Convert(Properties.Langs.Lang.Text, null, i, CultureInfo.CurrentCulture)}{i}");
list.Add($"{converter.Convert(Lang.Text, null, i, CultureInfo.CurrentCulture)}{i}");
}
return list;
@ -279,23 +280,23 @@ namespace HandyControlDemo.Service
{
new StepBarDemoModel
{
Header = $"{Properties.Langs.Lang.Step}1",
Content = Properties.Langs.Lang.Register
Header = LangKeys.Step,
Content = LangKeys.Register
},
new StepBarDemoModel
{
Header = $"{Properties.Langs.Lang.Step}2",
Content = Properties.Langs.Lang.BasicInfo
Header = LangKeys.Step,
Content = LangKeys.BasicInfo
},
new StepBarDemoModel
{
Header = $"{Properties.Langs.Lang.Step}3",
Content = Properties.Langs.Lang.UploadFile
Header = LangKeys.Step,
Content = LangKeys.UploadFile
},
new StepBarDemoModel
{
Header = $"{Properties.Langs.Lang.Step}4",
Content = Properties.Langs.Lang.Complete
Header = LangKeys.Step,
Content = LangKeys.Complete
}
};
}
@ -374,7 +375,7 @@ namespace HandyControlDemo.Service
foreach (var item in jsonObj)
{
var titleKey = (string) item.title;
var title = Properties.Langs.Lang.ResourceManager.GetString(titleKey);
var title = titleKey;
var list = Convert2DemoItemList(item.demoItemList);
infoList.Add(new DemoInfoModel
{
@ -393,7 +394,7 @@ namespace HandyControlDemo.Service
foreach (var item in list)
{
var name = Properties.Langs.Lang.ResourceManager.GetString((string)item[0]);
var name = (string)item[0];
string targetCtlName = item[1];
string imageName = item[2];
var isNew = !string.IsNullOrEmpty((string)item[3]);

View File

@ -4,6 +4,9 @@ namespace HandyControlDemo.Tools.Extension
{
public class LangExtension : HandyControl.Tools.Extension.LangExtension
{
public override object LangSource => LangDecorator.Instance;
public LangExtension()
{
Source = LangDecorator.Instance;
}
}
}

View File

@ -7,13 +7,13 @@
Background="{DynamicResource RegionBrush}">
<hc:TransitioningContentControl>
<StackPanel Margin="32" Width="400">
<hc:OutlineText TextWrapping="Wrap" Fill="{DynamicResource PrimaryTextBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=20}"/>
<hc:OutlineText Margin="0,10,0,0" FontSize="30" Fill="{DynamicResource SuccessBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=8}"/>
<hc:OutlineText Margin="0,10,0,0" Fill="{DynamicResource PrimaryTextBrush}" FontSize="30" FontStyle="Italic" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=8}"/>
<hc:OutlineText Margin="0,10,0,0" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" FontSize="50" StrokeThickness="2" Fill="{DynamicResource DangerBrush}" Stroke="{DynamicResource PrimaryBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:OutlineText Margin="0,10,0,0" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:OutlineText Margin="0,10,0,0" StrokePosition="Outside" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:OutlineText Margin="0,10,0,0" StrokePosition="InSide" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:OutlineText TextWrapping="Wrap" Fill="{DynamicResource PrimaryTextBrush}" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=20}"/>
<hc:OutlineText Margin="0,10,0,0" FontSize="30" Fill="{DynamicResource SuccessBrush}" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=8}"/>
<hc:OutlineText Margin="0,10,0,0" Fill="{DynamicResource PrimaryTextBrush}" FontSize="30" FontStyle="Italic" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=8}"/>
<hc:OutlineText Margin="0,10,0,0" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" FontSize="50" StrokeThickness="2" Fill="{DynamicResource DangerBrush}" Stroke="{DynamicResource PrimaryBrush}" Text="ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:OutlineText Margin="0,10,0,0" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:OutlineText Margin="0,10,0,0" StrokePosition="Outside" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:OutlineText Margin="0,10,0,0" StrokePosition="InSide" FontSize="80" FontWeight="Bold" StrokeThickness="4" Fill="{DynamicResource PrimaryBrush}" Stroke="{DynamicResource DangerBrush}" Text="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
</StackPanel>
</hc:TransitioningContentControl>
</UserControl>

View File

@ -7,14 +7,14 @@
xmlns:ex="clr-namespace:HandyControlDemo.Tools.Extension">
<hc:TransitioningContentControl>
<StackPanel Margin="32" VerticalAlignment="Center">
<hc:RunningBlock Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:RunningBlock Runaway="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoRun="True" Width="100" Runaway="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Duration="0:0:2" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoReverse="True" AutoRun="True" Width="100" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoRun="True" Width="200" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:10" Margin="0,16,0,0"/>
<hc:RunningBlock AutoReverse="True" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:10" Margin="0,16,0,0"/>
<hc:RunningBlock Orientation="Vertical" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:4" Margin="0,16,0,0"/>
<hc:RunningBlock Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:RunningBlock Runaway="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoRun="True" Width="100" Runaway="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Duration="0:0:2" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoReverse="True" AutoRun="True" Width="100" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock HorizontalAlignment="Left" AutoRun="True" Width="200" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}" Margin="0,16,0,0"/>
<hc:RunningBlock Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:10" Margin="0,16,0,0"/>
<hc:RunningBlock AutoReverse="True" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:10" Margin="0,16,0,0"/>
<hc:RunningBlock Orientation="Vertical" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=10}" FontSize="20" FontWeight="Bold" Foreground="OrangeRed" Duration="0:0:4" Margin="0,16,0,0"/>
<hc:RunningBlock Margin="0,16,0,0" Duration="0:0:20" IsRunning="{Binding IsMouseOver,RelativeSource={RelativeSource Self},Converter={StaticResource Boolean2BooleanReConverter}}">
<StackPanel Orientation="Horizontal">
<hc:GifImage Uri="/HandyControlDemo;component/Resources/Img/QQ/1.gif" Margin="10,0" Width="30" Height="30"/>

View File

@ -39,8 +39,11 @@
<hc:StepBar.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Header}" FontSize="16" FontWeight="Bold" HorizontalAlignment="Left"/>
<TextBlock Margin="0,4,0,0" Text="{Binding Content}"/>
<TextBlock FontSize="16" FontWeight="Bold" HorizontalAlignment="Left">
<Run Text="{ex:Lang Key={Binding Header}}"/>
<Run Text="{Binding Index,RelativeSource={RelativeSource AncestorType=hc:StepBarItem}}"/>
</TextBlock>
<TextBlock Margin="0,4,0,0" Text="{ex:Lang Key={Binding Content}}"/>
</StackPanel>
</DataTemplate>
</hc:StepBar.ItemTemplate>
@ -49,8 +52,11 @@
<hc:StepBar.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Header}" FontSize="16" FontWeight="Bold" HorizontalAlignment="Left"/>
<TextBlock Margin="0,4,0,0" Text="{Binding Content}"/>
<TextBlock FontSize="16" FontWeight="Bold" HorizontalAlignment="Left">
<Run Text="{ex:Lang Key={Binding Header}}"/>
<Run Text="{Binding Index,RelativeSource={RelativeSource AncestorType=hc:StepBarItem}}"/>
</TextBlock>
<TextBlock Margin="0,4,0,0" Text="{ex:Lang Key={Binding Content}}"/>
</StackPanel>
</DataTemplate>
</hc:StepBar.ItemTemplate>

View File

@ -7,16 +7,16 @@
xmlns:hc="https://handyorg.github.io/handycontrol">
<hc:TransitioningContentControl>
<hc:TagPanel AddTagButtonClick="TagPanel_OnAddTagButtonClick" Margin="32" Orientation="Horizontal" MaxWidth="400" VerticalAlignment="Center" ShowAddButton="True">
<hc:Tag ShowCloseButton="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:Tag Selectable="True" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:Tag IsSelected="True" Selectable="True" ShowCloseButton="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:Tag IsSelected="True" Selectable="True" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:Tag ShowCloseButton="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:Tag Selectable="True" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
<hc:Tag IsSelected="True" Selectable="True" ShowCloseButton="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=2}"/>
<hc:Tag IsSelected="True" Selectable="True" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=3}"/>
<hc:Tag ShowCloseButton="False" Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=4}"/>
<hc:Tag Content="{ex:Lang Key={x:Static langs:LangKeys.Text}, Converter={StaticResource StringRepeatConverter}, ConverterParameter=5}"/>
</hc:TagPanel>
</hc:TransitioningContentControl>
</UserControl>

View File

@ -1,5 +1,6 @@
using System;
using HandyControl.Controls;
using HandyControlDemo.Properties.Langs;
namespace HandyControlDemo.UserControl
{
@ -14,10 +15,9 @@ namespace HandyControlDemo.UserControl
{
if (sender is TagPanel panel)
{
panel.Children.Add(new Tag
{
Content = Properties.Langs.Lang.SubTitle
});
var tag = new Tag();
LangDecorator.SetLang(tag, ContentProperty, LangKeys.SubTitle);
panel.Children.Add(tag);
}
}
}

View File

@ -30,7 +30,7 @@
<TabControl SelectionChanged="TabControl_OnSelectionChanged" ItemContainerStyle="{StaticResource TabItemTransparent}" ItemsSource="{Binding DemoInfoList}" Style="{StaticResource TabControlInLine}" Background="Transparent" SelectedIndex="0">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}"/>
<TextBlock Text="{ex:Lang Key={Binding Title}}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
@ -51,7 +51,7 @@
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="{Binding ImageName}"/>
<TextBlock Margin="6,0,0,0" Text="{Binding Name}"/>
<TextBlock Margin="6,0,0,0" Text="{ex:Lang Key={Binding Name}}"/>
<Path Visibility="{Binding IsNew,Converter={StaticResource Boolean2VisibilityConverter}}" VerticalAlignment="Center" Data="{StaticResource NewGeometry}" Stretch="Uniform" Margin="10,0,0,0" Width="30" Height="14" Fill="{DynamicResource DangerBrush}"/>
</StackPanel>
</DataTemplate>

View File

@ -1,9 +1,8 @@
using System.Diagnostics;
using System.Globalization;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools;
using HandyControlDemo.Data;
using HandyControlDemo.Properties.Langs;
using HandyControlDemo.Window;
@ -23,19 +22,11 @@ namespace HandyControlDemo.UserControl
{
PopupConfig.IsOpen = false;
if (tag.Equals(GlobalData.Config.Lang)) return;
Growl.Ask(Properties.Langs.Lang.ChangeLangAsk, b =>
{
if (!b) return true;
GlobalData.Config.Lang = tag;
GlobalData.Save();
ConfigHelper.Instance.SetLang(tag);
LangDecorator.Culture = new CultureInfo(tag);
LangDecorator.Culture = new CultureInfo(tag);
//var processModule = Process.GetCurrentProcess().MainModule;
//if (processModule != null)
// Process.Start(processModule.FileName);
//Application.Current.Shutdown();
return true;
});
GlobalData.Config.Lang = tag;
GlobalData.Save();
}
}

View File

@ -7,12 +7,12 @@
Background="{DynamicResource RegionBrush}">
<hc:TransitioningContentControl>
<StackPanel Margin="32">
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelDanger}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelInfo}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelPrimary}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelSuccess}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelWarning}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelDanger}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelInfo}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelPrimary}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelSuccess}"/>
<Label HorizontalAlignment="Left" Margin="5" Content="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource LabelWarning}"/>
</StackPanel>
</hc:TransitioningContentControl>
</UserControl>

View File

@ -9,10 +9,10 @@
<RichTextBox Margin="32" Width="400" Height="300" ScrollViewer.VerticalScrollBarVisibility="Auto">
<FlowDocument>
<Paragraph>
<Run FontSize="20" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20,Mode=OneWay}"/>
<Run FontSize="20" Text="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=20,Mode=OneWay}"/>
</Paragraph>
<Paragraph>
<Run Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=1000,Mode=OneWay}"/>
<Run Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=1000,Mode=OneWay}"/>
</Paragraph>
<Paragraph>
<Hyperlink>https://github.com/NaBian/HandyControl</Hyperlink>

View File

@ -27,23 +27,23 @@
<TextBlock HorizontalAlignment="Left" Margin="5" Text="TextBlockDefaultSuccess" Style="{StaticResource TextBlockDefaultSuccess}"/>
</StackPanel>
<StackPanel Margin="10,0,0,0">
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockLargeBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockLarge}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=12}" Style="{StaticResource TextBlockTitleBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Title}},Converter={StaticResource StringRepeatConverter},ConverterParameter=12}" Style="{StaticResource TextBlockTitle}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.SubTitle}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockSubTitleBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.SubTitle}},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockSubTitle}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockLargeBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockLarge}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=12}" Style="{StaticResource TextBlockTitleBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Title},Converter={StaticResource StringRepeatConverter},ConverterParameter=12}" Style="{StaticResource TextBlockTitle}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.SubTitle},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockSubTitleBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.SubTitle},Converter={StaticResource StringRepeatConverter},ConverterParameter=10}" Style="{StaticResource TextBlockSubTitle}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefault}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultAccent}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultSecLight}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultThiLight}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultPrimary}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultDanger}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultWarning}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultInfo}"/>
<TextBlock hc:TextBlockAttach.AutoTooltip="True" Width="300" HorizontalAlignment="Left" Margin="5" Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultSuccess}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultBold}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefault}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultAccent}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultSecLight}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultThiLight}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultPrimary}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultDanger}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultWarning}"/>
<TextBlock HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultInfo}"/>
<TextBlock hc:TextBlockAttach.AutoTooltip="True" Width="300" HorizontalAlignment="Left" Margin="5" Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=20}" Style="{StaticResource TextBlockDefaultSuccess}"/>
</StackPanel>
</StackPanel>
</hc:TransitioningContentControl>

View File

@ -21,7 +21,7 @@
</ToolBar>
<ToolBar Band="1" Padding="6">
<ComboBox ItemsSource="{Binding DataList}" SelectedIndex="0" Width="230"/>
<TextBox Text="{Binding Source={ex:Lang Key={x:Static langs:LangKeys.Text}},Converter={StaticResource StringRepeatConverter},ConverterParameter=4,Mode=OneWay}" Margin="10,0,0,0" Width="230"/>
<TextBox Text="{ex:Lang Key={x:Static langs:LangKeys.Text},Converter={StaticResource StringRepeatConverter},ConverterParameter=4,Mode=OneWay}" Margin="10,0,0,0" Width="230"/>
</ToolBar>
<ToolBar Band="0" Padding="6">
<Menu Background="Transparent" VerticalAlignment="Center">

View File

@ -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>())

View File

@ -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));

View File

@ -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){#>

View File

@ -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
});
}
}
}

View File

@ -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);
}