Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Assemblies
.NET Core
.NET Standard
Active Directory
ADO.NET
Agile Development
AJAX
Alexa Skills
Algorithms in C#
Android
Angular
Architecture
ArcObject
Artificial Intelligence
ASP.NET
ASP.NET Core
Augmented Reality
Aurelia
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blockchain
Bootstrap
Bot Framework
Business
C#
C# Corner
C, C++, MFC
Career Advice
Chapters
CIO
Cloud
COBOL.NET
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Cortana Development
Cryptocurrency
Cryptography
Crystal Reports
Current Affairs
Custom Controls
Cyber Security
Data Mining
Databases & DBA
Design Patterns & Practices
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
Expression Studio
F#
Files, Directory, IO
Games Programming
GDI+
General
Google Cloud
Google Development
Graphics Design
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Internet & Web
Internet of Things
Ionic
iOS
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
kotlin
Leadership
Learn .NET
LightSwitch
LINQ
Machine Learning
Microsoft 365
Microsoft Office
Microsoft Phone
Mobile Development
Multithreading
NetBeans
Networking
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Power BI
Printing in C#
Products
Progressive Web Apps
Project Management
Python
Q#
QlikView
R
React
Reports using C#
Robotics & Hardware
Ruby on Rails
Salesforce
Security
Servers
SharePoint
SignalR
Silverlight
Smart Devices
Software Testing
SQL Language
SQL Server
Startups
String in C#
Swift
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
WCF
Wearables
Web Development
Web Services
Web3
Windows 10
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML Standard
XML
XNA
XSharp
Register
Login
2
Answers
language reosurce combobox not working as it should do
Abbas Hamza
8y
258
1
Reply
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;
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
Why there is need of value type and ref type
how to reverse a words in a string