wpf数据绑定:通过datacontext更新不同视图中的目标

时间:2015-02-26 22:47:27

标签: c# wpf xaml data-binding multilingual

我正在尝试为我的modernui wpf应用程序实现语言选择系统。当语言从组合框切换时,这种改变将通过数据绑定传播到所有应用程序控件。 我按如下方式构建了骨架:

  • 语言是在List
  • 中收集的对象
  • 字符串及其对应项存储在静态字典中
  • 绑定数据由翻译对象提取
  • 视图绑定到基于语言框架的datacontext

以下是功能的精简版本和示例vs2013项目的链接。我尝试了INotify ..没有成功,我只能通过重置视图的datacontext(page1.xaml)来设法更新绑定目标。遗憾的是,无法在其他视图(home.xaml)上更新目标。问题归结为:"如何同时更新所有视图中的所有目标?" 我将非常感谢您为建立正确的绑定结构提供任何帮助和建议。

下载示例项目:http://goo.gl/yjSsKm

home.xaml

<Grid Style="{StaticResource ContentRoot}">
    <ScrollViewer>
        <StackPanel MinWidth="200">
            <TextBlock Text="{Binding home_text_1}"/>
        </StackPanel>
    </ScrollViewer>
</Grid>

的Page1.xaml

<Grid Style="{StaticResource ContentRoot}">
    <ScrollViewer>
        <StackPanel MinWidth="200">
            <TextBlock Text="{Binding page1_text_1}"/>
            <ComboBox x:Name="cbox_lang" Width="120" HorizontalAlignment="Left" Margin="0,50,0,0" SelectionChanged="cbox_lang_SelectionChanged"/>
        </StackPanel>
    </ScrollViewer>
</Grid>

MainWindow.xaml.cs

using DynamicDataBinding.Pages;
using FirstFloor.ModernUI.Windows.Controls;

namespace DynamicDataBinding
{
    public partial class MainWindow : ModernWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Language lang_1 = new Language("Language 1");
            Language lang_2 = new Language("Language 2");
            Global.availableLanguages.Add(lang_1);
            Global.availableLanguages.Add(lang_2);
            Global.currentLanguage = lang_1;
            Global.currentLanguage.set();
        }
    }
}

page1.xaml.cs

using System.Collections.Generic;
using System.Windows.Controls;

namespace DynamicDataBinding.Pages
{
    public partial class Page1 : UserControl
    {
        public Page1()
        {
            InitializeComponent();
            foreach (Language lang in Global.availableLanguages) 
            {
                cbox_lang.Items.Add(lang.Name);
            }
            cbox_lang.SelectedItem = Global.currentLanguage.Name;
            this.DataContext = Global.currentLanguage.FrameWork;
        }

        private void cbox_lang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbox_lang.SelectedItem.ToString() != Global.currentLanguage.Name)
            {
                string selectedLanguage = cbox_lang.SelectedItem.ToString();
                Global.currentLanguage = Global.availableLanguages.Find(lang => lang.Name == selectedLanguage);
                Global.currentLanguage.set();
                DataContext = null;
                DataContext = Global.currentLanguage.FrameWork;
            }
        }
    }

    public class Global
    {
        public static Dictionary<string, string> dictionary = new Dictionary<string, string>();
        public static List<Language> availableLanguages = new List<Language>();
        public static Language currentLanguage;
    }

    public class Language
    {
        public string Name;
        public Translation FrameWork; 

        public Language(string name)
        {
            this.Name = name;
        }

        public void set()
        {
            Global.dictionary.Clear();
            if (Global.currentLanguage.Name == "Language 1")
            {
                Global.dictionary.Add("home_content_1", "Content For Home in Language 1");
                Global.dictionary.Add("page1_content_1", "Content For Page1 in Language 1");
            }
            else if (Global.currentLanguage.Name == "Language 2")
            {
                Global.dictionary.Add("home_content_1", "Different Content For Home in Language 2");
                Global.dictionary.Add("page1_content_1", "Different Content For Page1 in Language 2");
            }
            FrameWork = new Translation();
        }
    }

    public class Translation
    {
        public string home_text_1 { get { return Global.dictionary["home_content_1"]; } }
        public string page1_text_1 { get { return Global.dictionary["page1_content_1"]; } }
    }
}

home.xaml.cs

using System.Windows.Controls;
namespace DynamicDataBinding.Pages
{
    public partial class Home : UserControl
    {
        public Home()
        {
            InitializeComponent();
            this.DataContext = Global.currentLanguage.FrameWork;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在一天结束时(最终)我发现没有必要处理datacontext。如果设置正确,实现INotifyPropertyChanged将处理所有目标更新。此外,如果绑定有问题,则使用字典不适合保存数据。以下是代码的更正版本:

page1.xaml.cs

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;

namespace DynamicDataBinding.Pages
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : UserControl
    {
        public Page1()
        {
            InitializeComponent();
            foreach (Language lang in Global.availableLanguages) 
            {
                cbox_lang.Items.Add(lang.Name);
            }
            cbox_lang.SelectedItem = Global.currentLanguage.Name;
            this.DataContext = Global.FrameWork;
        }

        private void cbox_lang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbox_lang.SelectedItem.ToString() != Global.currentLanguage.Name)
            {
                string selectedLanguage = cbox_lang.SelectedItem.ToString();
                Global.currentLanguage = Global.availableLanguages.Find(lang => lang.Name == selectedLanguage);
                Global.currentLanguage.set();
                DataContext = null;
                DataContext = Global.FrameWork;
            }
        }
    }

    public class Global
    {
        public static Dictionary<string, string> dictionary = new Dictionary<string, string>();
        public static List<Language> availableLanguages = new List<Language>();
        public static Language currentLanguage;
        public static Translation FrameWork = new Translation();
    }

    public class Language
    {

        public string Name;
        public Language(string name)
        {
            this.Name = name;
        }

        public void set()
        {
            Global.dictionary.Clear();
            if (Global.currentLanguage.Name == "Language 1")
            {
                Global.FrameWork.home_text_1 = "Content For Home in Language 1";
                Global.FrameWork.page1_text_1 = "Content For Home in Language 1";

            }
            else if (Global.currentLanguage.Name == "Language 2")
            {
                Global.FrameWork.home_text_1 = "Different Content For Home in Language 2";
                Global.FrameWork.page1_text_1 = "Different Content For Home in Language 2";
            }
        }
    }

    public class Translation : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string _home_text_1;
        private string _page1_text_1;

        public string home_text_1 { get {return _home_text_1;} set { _home_text_1 = value; OnPropertyChanged("home_text_1"); }}
        public string page1_text_1 { get {return _page1_text_1;} set { _page1_text_1 = value; OnPropertyChanged("page1_text_1");}}
    }
}