Created
May 5, 2018 15:44
-
-
Save christiannagel/168606f308f0e557a2e3bb2cee42626c to your computer and use it in GitHub Desktop.
CulturesViewModel - initialize a list of cultures to display in a tree-view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CulturesViewModel : INotifyPropertyChanged | |
{ | |
public CulturesViewModel() => SetupCultures(); | |
// ... INotifyPropertyChanged Implementation | |
private void SetupCultures() | |
{ | |
var cultureDataDict = CultureInfo.GetCultures(CultureTypes.AllCultures) | |
.OrderBy(c => c.Name) | |
.Select(c => new CultureData | |
{ | |
CultureInfo = c, | |
SubCultures = new List<CultureData>() | |
}) | |
.ToDictionary(c => c.CultureInfo.Name); | |
var rootCultures = new List<CultureData>(); | |
foreach (var cd in cultureDataDict.Values) | |
{ | |
if (cd.CultureInfo.Parent.LCID == 0x7f) // check for invariant culture | |
{ | |
rootCultures.Add(cd); | |
} | |
else // add to parent culture | |
{ | |
if (cultureDataDict.TryGetValue(cd.CultureInfo.Parent.Name, out CultureData parentCultureData)) | |
{ | |
parentCultureData.SubCultures.Add(cd); | |
} | |
else | |
{ | |
throw new InvalidOperationException("parent culture not found"); | |
} | |
} | |
} | |
foreach (var rootCulture in rootCultures.OrderBy(cd => cd.CultureInfo.EnglishName)) | |
{ | |
RootCultures.Add(rootCulture); | |
} | |
} | |
public IList<CultureData> RootCultures { get; } = new List<CultureData>(); | |
private CultureData _selectedCulture; | |
public CultureData SelectedCulture | |
{ | |
get => _selectedCulture; | |
set => SetProperty(ref _selectedCulture, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment