TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abbas Hamza
NA
100
53k
language reosurce combobox not working as it should do
Aug 9 2016 6:10 AM
Hi Guys,
I'm working in a WPF C# application that has a combobox with 2 language selection. i have found a tutorial online that do exactly what i want in term of changing language resource, but the problem is that i have many WPF windows and all of them have a button link link to the main Window that launched when application is fired where the combobox is available. the language selection is working fine when first launched, but the problem arise when you navigate from the main window to another window and when clicked on home button the language selected is always English although i have changed it to a different language and if i need to to change back to English I need to click on the other language then click back on English to revert to English:
How can make the application detect the current resource dictionary automatically and change the combobox to reflect the selected language?
here is some of the code:
private
void
ddlLanguage_SelectionChanged(
object
sender, SelectionChangedEventArgs e)
{
var currentResourceDictionary = (from d
in
BaseModel.Instance.ImportCatalog.ResourceDictionaryList
where d.Metadata.ContainsKey(
"Culture"
)
&& d.Metadata[
"Culture"
].ToString().Equals(vm.SelectedLanguage.Code)
select d).FirstOrDefault();
if
(currentResourceDictionary !=
null
)
{
var previousResourceDictionary = (from d
in
BaseModel.Instance.ImportCatalog.ResourceDictionaryList
where d.Metadata.ContainsKey(
"Culture"
)
&& d.Metadata[
"Culture"
].ToString().Equals(vm.PreviousLanguage.Code)
select d).FirstOrDefault();
if
(previousResourceDictionary !=
null
&& previousResourceDictionary != currentResourceDictionary)
{
Application.Current.Resources.MergedDictionaries.Remove(previousResourceDictionary.Value);
Application.Current.Resources.MergedDictionaries.Add(currentResourceDictionary.Value);
CultureInfo cultureInfo =
new
CultureInfo(vm.SelectedLanguage.Code);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Application.Current.MainWindow.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
vm.PreviousLanguage = vm.SelectedLanguage;
}
}
}
in the view model :
public
MainViewModel()
{
LoadResources();
SelectedLanguage = LanguageList.FirstOrDefault();
PreviousLanguage = SelectedLanguage;
}
load resource;
private
void
LoadResources()
{
LanguageList =
new
List<Languages>();
LanguageList.Add(
new
Languages() { Code =
"en-GB"
, Name =
"English"
});
LanguageList.Add(
new
Languages() { Code =
"cy-GB"
, Name =
"Cymraeg"
});
}
in the main Window constructor:
MainViewModel vm;
public
MainMenu()
{
InitializeComponent();
vm =
new
MainViewModel();
this
.DataContext = vm;
}
Reply
Answers (
2
)
Why there is need of value type and ref type
how to reverse a words in a string