Click here to Skip to main content
15,890,185 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Switch between diffrent languages with indexers Pin
Mc_Topaz1-Sep-11 2:23
Mc_Topaz1-Sep-11 2:23 
GeneralRe: Switch between diffrent languages with indexers Pin
Pete O'Hanlon1-Sep-11 3:27
mvePete O'Hanlon1-Sep-11 3:27 
GeneralRe: Switch between different languages with indexers Pin
Wayne Gaylard1-Sep-11 21:50
professionalWayne Gaylard1-Sep-11 21:50 
GeneralRe: Switch between different languages with indexers Pin
Pete O'Hanlon1-Sep-11 22:27
mvePete O'Hanlon1-Sep-11 22:27 
GeneralRe: Switch between different languages with indexers Pin
Wayne Gaylard1-Sep-11 23:05
professionalWayne Gaylard1-Sep-11 23:05 
GeneralRe: Switch between different languages with indexers Pin
Pete O'Hanlon1-Sep-11 23:29
mvePete O'Hanlon1-Sep-11 23:29 
GeneralRe: Switch between different languages with indexers Pin
Mycroft Holmes1-Sep-11 23:45
professionalMycroft Holmes1-Sep-11 23:45 
GeneralRe: Switch between diffrent languages with indexers Pin
Wayne Gaylard1-Sep-11 2:39
professionalWayne Gaylard1-Sep-11 2:39 
This is how I would do it (only if my requirements stated I had to use your language files - otherwise I would use Resource Files as Abhinav suggestes)

Your LanguageItem class:

C#
class LanguageItem
   {
       public int ID { get; set; }
       public string Word { get; set; }
   }



A separate class which I will call LanguageViewModel:

class LanguageViewModel : INotifyPropertyChanged
    {
        enum LanguageChoice
        {
            UK,
            US
        }

        private LanguageChoice selectedLanguage = LanguageChoice.UK;
        private string languageText = "Switch to US English";

        public string LanguageText
        {
            get
            {
                return languageText;
            }
            set
            {
                if (languageText != value)
                {
                    languageText = value;
                    NotifyPropertyChanged("LanguageText");
                }
             }
        }

        ObservableCollection<LanguageItem> words = new ObservableCollection<LanguageItem>();
        public ObservableCollection<LanguageItem> Words
        {
            get
            {
                return words;
            }
            set
            {
                if (words != value)
                {
                    words = value;
                    NotifyPropertyChanged("Words");
                }
            }
        }

        public LanguageViewModel()
        {
            LoadLanguage(selectedLanguage);
        }

        private RelayCommand switchCommand;
        public ICommand SwitchCommand
        {
            get
            {
                return switchCommand ?? (switchCommand = new RelayCommand(() => ObeySwitchCommand()));
            }
        }

        private void ObeySwitchCommand()
        {
            selectedLanguage = selectedLanguage == LanguageChoice.UK ? LanguageChoice.US : LanguageChoice.UK;
            LanguageText = selectedLanguage == LanguageChoice.UK ? "Switch to US English" : "Switch to UK English";
            LoadLanguage(selectedLanguage);
        }

        void LoadLanguage(LanguageChoice languageToLoad)
        {
            string path;
            if (languageToLoad == LanguageChoice.UK)
            {
                path = @"C:\Users\SwitchLanguage\UK.txt";
            }
            else
            {
                path = @"C:\Users\SwitchLanguage\US.txt";
            }
            StreamReader sr = new StreamReader(path);
            ObservableCollection<LanguageItem> newWords = new ObservableCollection<LanguageItem>();
            while (!sr.EndOfStream)
            {
                string[] parts = sr.ReadLine().Split(new char[] { ',' });
                int index;
                int.TryParse(parts[0], out index);
                LanguageItem li = new LanguageItem() { ID = index, Word = parts[1] };
                newWords.Add(li);
            }
            Words = newWords;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }


My XAML File (Note no code in code behind):

XML
<Grid.ColumnDefinitions>
           <ColumnDefinition />
           <ColumnDefinition />
       </Grid.ColumnDefinitions>
       <Grid.RowDefinitions>
           <RowDefinition />
           <RowDefinition />
       </Grid.RowDefinitions>
       <Label Content="{Binding Path=Words[0].Word}"/>
       <Label Content="{Binding Path=Words[1].Word}" Grid.Column="1"/>
       <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom"
               Command="{Binding Path=SwitchCommand}" Content="{Binding Path=LanguageText}"
               Width="100" Height="23"/>


Then I would override the OnStartUp method in the App.xaml.cs file like this:

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LanguageViewModel vm = new LanguageViewModel();
            LanguageTest lt = new LanguageTest();
            lt.DataContext = vm;
            lt.ShowDialog();
        }


And here is a bog standard RelayCommand class that is used for binding the Button's click command to the SwitchCommand on the ViewModel:


public class RelayCommand : ICommand
    {
        readonly Action execute;
        readonly Func<bool> canExecute;

        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            this.execute = execute;
            this.canExecute = canExecute;
        }
        
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute();
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object parameter)
        {
            execute();
        }
    }

Live for today. Plan for tomorrow. Party tonight!

AnswerRe: Switch between diffrent languages with indexers Pin
Abhinav S1-Sep-11 1:13
Abhinav S1-Sep-11 1:13 
Questionsilverlight - backgroundworker process [modified] Pin
arkiboys31-Aug-11 18:41
arkiboys31-Aug-11 18:41 
AnswerRe: silverlight - backgroundworker process Pin
Mycroft Holmes31-Aug-11 19:28
professionalMycroft Holmes31-Aug-11 19:28 
GeneralRe: silverlight - backgroundworker process Pin
arkiboys31-Aug-11 19:30
arkiboys31-Aug-11 19:30 
AnswerRe: silverlight - backgroundworker process Pin
Pete O'Hanlon31-Aug-11 21:07
mvePete O'Hanlon31-Aug-11 21:07 
GeneralRe: silverlight - backgroundworker process Pin
arkiboys31-Aug-11 22:11
arkiboys31-Aug-11 22:11 
GeneralRe: silverlight - backgroundworker process Pin
Pete O'Hanlon31-Aug-11 23:18
mvePete O'Hanlon31-Aug-11 23:18 
GeneralRe: silverlight - backgroundworker process Pin
arkiboys31-Aug-11 23:38
arkiboys31-Aug-11 23:38 
GeneralRe: silverlight - backgroundworker process Pin
Pete O'Hanlon1-Sep-11 0:12
mvePete O'Hanlon1-Sep-11 0:12 
GeneralRe: silverlight - backgroundworker process Pin
arkiboys1-Sep-11 0:16
arkiboys1-Sep-11 0:16 
QuestionWindows Phone Pin
arkiboys31-Aug-11 7:43
arkiboys31-Aug-11 7:43 
QuestionMaster-Details binding of 2 related tables Pin
bitwise.csc31-Aug-11 2:29
bitwise.csc31-Aug-11 2:29 
AnswerRe: Master-Details binding of 2 related tables Pin
Wayne Gaylard31-Aug-11 4:32
professionalWayne Gaylard31-Aug-11 4:32 
GeneralRe: Master-Details binding of 2 related tables Pin
bitwise.csc31-Aug-11 4:59
bitwise.csc31-Aug-11 4:59 
GeneralRe: Master-Details binding of 2 related tables Pin
Pete O'Hanlon31-Aug-11 5:15
mvePete O'Hanlon31-Aug-11 5:15 
GeneralRe: Master-Details binding of 2 related tables Pin
bitwise.csc31-Aug-11 5:21
bitwise.csc31-Aug-11 5:21 
QuestionNavigate in Frame from code Pin
yesu prakash30-Aug-11 19:34
yesu prakash30-Aug-11 19:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.