Binding to CultureInfos
Hello, I'd like to to bind ComboBox to all possible CultureInfo instances, for example toSystem.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures) array. Is it possible from XAML or do I have to do it from code?
How to effectively filter the results? For example include only cultures starting with en- ?
Hi,
Binding to all cultures is quite easy in XAML only:
Code Snippet
<
Window x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="300" Width="300" xmlns:global="clr-namespace:System.Globalization;assembly=mscorlib" >
<
Window.Resources> <
ObjectDataProvider x:Key="CulturesProvider" ObjectType="{x:Type global:CultureInfo}" MethodName="GetCultures"> <
ObjectDataProvider.MethodParameters> <
global:CultureTypes>AllCultures</global:CultureTypes> </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<
Grid> <
ListBox ItemsSource="{Binding Source={StaticResource CulturesProvider}}"/> </Grid>
</Window>
Grouping and sorting can be done in XAML too. For example, here is the same output, this time sorting according to its culture tag (for example en-US).
Code Snippet
<
Window x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="300" Width="300" xmlns:global="clr-namespace:System.Globalization;assembly=mscorlib" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" >
<
Window.Resources> <
ObjectDataProvider x:Key="CulturesProvider" ObjectType="{x:Type global:CultureInfo}" MethodName="GetCultures"> <
ObjectDataProvider.MethodParameters> <
global:CultureTypes>AllCultures</global:CultureTypes> </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<
CollectionViewSource x:Key="MyCVS" Source="{StaticResource CulturesProvider}"> <
CollectionViewSource.SortDescriptions> <
scm:SortDescription PropertyName="IetfLanguageTag" /> </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<
Grid> <
ListBox ItemsSource="{Binding Source={StaticResource MyCVS}}"/> </Grid>
</Window>
As for filtering, however, you need to create an event handler, so of course you need some code behind. This displays only the culture of whose the tag starts with en-
Code Snippet
<
Window x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="300" Width="300" xmlns:global="clr-namespace:System.Globalization;assembly=mscorlib" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" >
<
Window.Resources> <
ObjectDataProvider x:Key="CulturesProvider" ObjectType="{x:Type global:CultureInfo}" MethodName="GetCultures"> <
ObjectDataProvider.MethodParameters> <
global:CultureTypes>AllCultures</global:CultureTypes> </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<
CollectionViewSource x:Key="MyCVS" Source="{StaticResource CulturesProvider}" Filter="MyCVS_Filter"> <
CollectionViewSource.SortDescriptions> <
scm:SortDescription PropertyName="IetfLanguageTag" /> </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<
Grid> <
ListBox ItemsSource="{Binding Source={StaticResource MyCVS}}"/> </Grid>
</Window>
with:
Code Snippet
void MyCVS_Filter(object sender, FilterEventArgs e)
{
CultureInfo item = e.Item as CultureInfo;
if (item.IetfLanguageTag.StartsWith("en-"))
{
e.Accepted = true;
}
else
{
e.Accepted =
false; }
}
HTH,
Laurent