在WPF窗体应用程序中,我采用的XAML的方法实现多语言,下面是页面的代码,DataGrid的字段只认设置的默认语言,其中Label是可以实现语言切换的。请各位大虾不吝赐教,小弟在此谢过~
<ComboBox Height="23" Name="comboBox1" Width="120" SelectionChanged="comboBox1_SelectionChanged" Margin="0,0,158,238">
<ComboBoxItem Content="中文" />
<ComboBoxItem Content="英文" />
</ComboBox>
<Label Content="{DynamicResource Label1}" Height="28" Name="label1" Margin="57,65,123,168" />
<DataGrid x:Name="dg_view" IsReadOnly="True" AutoGenerateColumns="False" Margin="0,180,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="{DynamicResource Language_no}" Binding="{Binding LANGUAGE_NO}"/>
<DataGridTextColumn Header="{DynamicResource Language_name}" Binding="{Binding LANGUAGE_NAME}"/>
</DataGrid.Columns>
</DataGrid>
后台代码: try
{
langRd =
Application.LoadComponent(
new Uri(@"Lang\zh-TW.xaml", UriKind.Relative))
as ResourceDictionary;
}
catch
{
}
if (langRd != null)
{
if (this.Resources.MergedDictionaries.Count > 0)
{
this.Resources.MergedDictionaries.Clear();
}
this.Resources.MergedDictionaries.Add(langRd);
}
看下这个例子:
XAML :
<Window.Resources>
<TextBlock x:Key="Label1"></TextBlock>
<TextBlock x:Key="Language_no"></TextBlock>
<TextBlock x:Key="Language_name"></TextBlock>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="260"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="{StaticResource Label1}" HorizontalAlignment="Right" Height="28" Name="label1" Grid.Column="0" Grid.Row="0"/>
<ComboBox Height="23" Name="comboBox1" HorizontalAlignment="Left" Width="120" SelectionChanged="comboBox1_SelectionChanged" Grid.Column="1" Grid.Row="0">
<ComboBoxItem Content="中文" />
<ComboBoxItem Content="英文" />
</ComboBox>
<DataGrid x:Name="dg_view" IsReadOnly="True" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn Header="{StaticResource Language_no}" Width="150"
Binding="{Binding no}" />
<DataGridTextColumn Header="{StaticResource Language_name}" Width="150"
Binding="{Binding name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
CS:public MainWindow()数据源临时定义的, 你可以换成你读取文件得到的值。
{
InitializeComponent();
DataTable table = new DataTable();
table.Columns.Add("no");
table.Columns.Add("name");
DataRow row = null;
row = table.NewRow();
row[0] = "001";
row[1] = "零零一";
table.Rows.Add(row);
row = table.NewRow();
row[0] = "002";
row[1] = "零零二";
table.Rows.Add(row);
this.dg_view.DataContext = table;
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TextBlock Label = FindResource("Label1") as TextBlock;
TextBlock Language_no = FindResource("Language_no") as TextBlock;
TextBlock Language_name = FindResource("Language_name") as TextBlock;
ComboBox c = e.Source as ComboBox;
if (c.Text == "英文" || c.Text==string.Empty)
{
Label.Text = "标签";
Language_no.Text = "007";
Language_name.Text = "国产零零七";
}
else
{
Label.Text = "Label";
Language_no.Text = "007";
Language_name.Text = "007";
}
}
参考:http://www.cnblogs.com/coogle/archive/2008/07/18/1245860.html
图片不显示了。