none
How to update I18N immediately in WPF? RRS feed

  • 问题

  • I make a I18N demonstration in WPF. I create two Resources.resx, one is "Resources.resx" , the other is "Resources.zh-CN.resx".

    I want to change the control's text when changed CultureInfo .

    Now I can load resources from Resources.XXX.resx file. but I couldn't dynamic change the contorls' text.

    <Window x:Class="Demo05_I18N.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:Demo05_I18N"
        xmlns:prop="clr-namespace:Demo05_I18N.Properties"
        Title="MainWindow" Height="176" Width="326">
      <Window.Resources>
        <local:StringList x:Key="CultureList">
          <sys:String>zh-CN</sys:String>
          <sys:String>en</sys:String>
        </local:StringList>
      </Window.Resources>
      <Grid Margin="5">
        <Grid.Resources>
          <Style x:Key="CellControlStyle" TargetType="{x:Type Control}">
            <Setter Property="Margin" Value="5" />
          </Style>
          <Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="5" />
          </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="120"/>
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Name="txtLoginName" Style="{StaticResource CellTextStyle}" Grid.Row="0" Grid.Column="0" Text="{x:Static prop:Resources.LoginName}"/>
        <TextBlock Style="{StaticResource CellTextStyle}" Grid.Row="1" Grid.Column="0" Text="{x:Static prop:Resources.LoginPwd}"/>
        <TextBox Style="{StaticResource CellControlStyle}" Grid.Row="0" Grid.Column="1" />
        <TextBox Style="{StaticResource CellControlStyle}" Grid.Row="1" Grid.Column="1" />
        <Button Style="{StaticResource CellControlStyle}" Grid.Row="2" Grid.Column="1" Content="{x:Static prop:Resources.LoginButton}" />
    
        <TextBlock Style="{StaticResource CellTextStyle}" Grid.Row="3" Grid.Column="0" Text="{x:Static prop:Resources.SwitchCultureInfo}"/>
        <ComboBox Name="cboCulture" ItemsSource="{StaticResource CultureList}" SelectedIndex="0" Grid.Row="3" Grid.Column="1" SelectionChanged="cboCulture_SelectionChanged" />
      </Grid>
    </Window>
    
    
    using System.Windows;
    using System.Windows.Controls;
    using System.Globalization;
    
    namespace Demo05_I18N {
      /// <summary>
      /// MainWindow.xaml 的交互逻辑
      /// </summary>
      public partial class MainWindow : Window {
        public MainWindow() {
          InitializeComponent();
        }
    
        private void cboCulture_SelectionChanged(object sender, SelectionChangedEventArgs e) {
          if (this.IsLoaded) {
            string sltCulture = cboCulture.SelectedItem.ToString();
            Demo05_I18N.Properties.Resources.Culture = new CultureInfo(sltCulture);
            //txtLoginName.SetValue(TextBlock.TextProperty, Demo05_I18N.Properties.Resources.LoginName);<br/>        <strong>//?How to dynamic change?</strong><br/>      }
    
        }
      }
    }
    

    2011年4月22日 6:30

答案