简体   繁体   English

动态设置TextBlock的文本绑定

[英]Dynamically set TextBlock's text binding

I am attempting to write a multilingual application in Silverlight 4.0 and I at the point where I can start replacing my static text with dynamic text from a SampleData xaml file. 我试图在Silverlight 4.0中编写一个多语言应用程序,此时我可以开始用SampleData xaml文件中的动态文本替换静态文本。 Here is what I have: 这是我所拥有的:

My Database 我的资料库

<SampleData:something xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.MyDatabase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <SampleData:something.mysystemCollection>
    <SampleData:mysystem ID="1" English="Menu" German="Menü" French="Menu" Spanish="Menú" Swedish="Meny" Italian="Menu" Dutch="Menu" />
  </SampleData:something.mysystemCollection>
</SampleData:something>

My UserControl 我的用户控件

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    x:Class="Something.MyUC" d:DesignWidth="1000" d:DesignHeight="600">
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MyDatabase}}">
        <Grid Height="50" Margin="8,20,8,0" VerticalAlignment="Top" d:DataContext="{Binding mysystemCollection[1]}" x:Name="gTitle">
            <TextBlock x:Name="Title" Text="{Binding English}" TextWrapping="Wrap" Foreground="#FF00A33D" TextAlignment="Center" FontSize="22"/>
        </Grid>
    </Grid>
</UserControl>

As you can see, I have 7 languages that I want to deal with. 如您所见,我要处理7种语言。 Right now this loads the English version of my text just fine. 现在,这可以很好地加载我的文本的英语版本。 I have spent the better part of today trying to figure out how to change the binding in my code to swap this out when I needed (lets say when I change the language via drop down). 今天,我花费了大部分时间试图找出如何更改代码中的绑定以在需要时将其替换掉(可以说,当我通过下拉菜单更改语言时)。

It sounds like you're looking for code like this: 听起来您正在寻找这样的代码:

Title.SetBinding(TextProperty, new Binding { Path = new PropertyPath(language) });

All it does is create a new Binding for the language you requested and use it to replace the old binding for the Title's Text property. 它所做的只是为您请求的语言创建一个新的绑定,并用它替换“标题”的“文本”属性的旧绑定。

You are going about this the wrong way. 您正在以错误的方式进行操作。 Best practice for localization in Silverlight is to use resource files holding the translated keywords. Silverlight本地化的最佳做法是使用包含翻译后关键字的资源文件。 Here is some more info about this: 这是有关此的更多信息:

http://msdn.microsoft.com/en-us/library/cc838238%28VS.95%29.aspx http://msdn.microsoft.com/zh-CN/library/cc838238%28VS.95%29.aspx

EDIT: 编辑:

Here is an example where I use a helper class to hold the translated strings. 这是一个使用助手类保存转换后的字符串的示例。 These translations could then be loaded from just about anywhere. 然后可以从几乎任何地方加载这些翻译。 Static resource files, xml, database or whatever. 静态资源文件,xml,数据库或其他内容。 I made this in a hurry, so it is not very stable. 我很着急,所以它不是很稳定。 And it only switches between english and swedish. 并且仅在英语和瑞典语之间切换。

XAML: XAML:

<UserControl x:Class="SilverlightApplication13.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:SilverlightApplication13"
             mc:Ignorable="d"
             d:DesignWidth="640"
             d:DesignHeight="480">

    <UserControl.Resources>
        <local:TranslationHelper x:Key="TranslationHelper"></local:TranslationHelper>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <StackPanel>

            <TextBlock Margin="10"
                       Text="{Binding Home, Source={StaticResource TranslationHelper}}"></TextBlock>

            <TextBlock Margin="10"
                       Text="{Binding Contact, Source={StaticResource TranslationHelper}}"></TextBlock>

            <TextBlock Margin="10"
                       Text="{Binding Links, Source={StaticResource TranslationHelper}}"></TextBlock>

            <Button Content="English"
                    HorizontalAlignment="Left"
                    Click="BtnEnglish_Click"
                    Margin="10"></Button>

            <Button Content="Swedish"
                    HorizontalAlignment="Left"
                    Click="BtnSwedish_Click"
                    Margin="10"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Code-behind + TranslationHelper class: 后台代码+ TranslationHelper类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace SilverlightApplication13
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            //Default
            (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US");
        }

        private void BtnEnglish_Click(object sender, RoutedEventArgs e)
        {
            (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("en-US");
        }

        private void BtnSwedish_Click(object sender, RoutedEventArgs e)
        {
            (this.Resources["TranslationHelper"] as TranslationHelper).SetLanguage("sv-SE");
        }
    }

    public class TranslationHelper : INotifyPropertyChanged
    {
        private string _Contact;

        /// <summary>
        /// Contact Property
        /// </summary>
        public string Contact
        {
            get { return _Contact; }
            set
            {
                _Contact = value;
                OnPropertyChanged("Contact");
            }
        }

        private string _Links;

        /// <summary>
        /// Links Property
        /// </summary>
        public string Links
        {
            get { return _Links; }
            set
            {
                _Links = value;
                OnPropertyChanged("Links");
            }
        }

        private string _Home;

        /// <summary>
        /// Home Property
        /// </summary>
        public string Home
        {
            get { return _Home; }
            set
            {
                _Home = value;
                OnPropertyChanged("Home");
            }
        }



        public TranslationHelper()
        {
            //Default
            SetLanguage("en-US");
        }

        public void SetLanguage(string cultureName)
        {
            //Hard coded values, need to be loaded from db or elsewhere

            switch (cultureName)
            {
                case "sv-SE":
                    Contact = "Kontakt";
                    Links = "Länkar";
                    Home = "Hem";
                    break;

                case "en-US":
                    Contact = "Contact";
                    Links = "Links";
                    Home = "Home";
                    break;

                default:
                    break;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM