首页 新闻 会员 周边 捐助

WPF窗体应用程序中DataGrid如何实现多语言

0
悬赏园豆:20 [已解决问题] 解决于 2010-08-13 08:37

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

还是小混混的主页 还是小混混 | 初学一级 | 园豆:125
提问于:2010-07-29 09:05
< >
分享
最佳答案
0

看下这个例子:

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";
            }
     }
数据源临时定义的, 你可以换成你读取文件得到的值。
收获园豆:15
HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-11 16:45
这种办法好像不是很好用,如果我在后面再多添加一种语言的话会很麻烦~我现在用的是读取XML:Assembly assembly = Assembly.GetExecutingAssembly();//通过用户选择选取不同的XML Stream xmlStream= assembly.GetManifestResourceStream(“XXX.xml”); XmlDocument doc = new XmlDocument(); doc.Load(xmlStream); language_no.Header = doc.DocumentElement["DataGrid"]["Language_no"].InnerText; language_name.Header = doc.DocumentElement["DataGrid"]["Language_name"].InnerText
还是小混混 | 园豆:125 (初学一级) | 2010-08-13 08:42
这是个是简单的例子, 多加个语言的话,很容易啊,XAML :ComboBox 可以再加个选项:如 德语,再在ComboBox 的SelectionChanged 事件里面处理啊,根据选择的项来加载不同XML 文件啊。如下: if (c.Text=="中文") { Stream xmlStream= assembly.GetManifestResourceStream(“中文.xml”); } else if(c.Text=="英文") { Stream xmlStream= assembly.GetManifestResourceStream(“英文.xml”); } else { Stream xmlStream= assembly.GetManifestResourceStream(“德文.xml”); } 这个不就可以了么?
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-13 10:19
其他回答(1)
0

参考:http://www.cnblogs.com/coogle/archive/2008/07/18/1245860.html

图片不显示了。

收获园豆:5
Astar | 园豆:40805 (高人七级) | 2010-07-29 09:25
locbaml 不會使用,我這裡報錯。IOException was unhandled找不到資源 'window1.xaml'。
支持(0) 反对(0) 还是小混混 | 园豆:125 (初学一级) | 2010-07-29 14:16
最好是給個有DataGrid的Demo^_^
支持(0) 反对(0) 还是小混混 | 园豆:125 (初学一级) | 2010-07-29 14:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册