I need to select custom item under WPF Combobox by code behind. For example: “Spanish”
UILanguages languages = new UILanguages();
languages.Add(
new UILanguage
{
Culture = "en",
SpecCulture = "en-US",
EnglishName = "English"
});
languages.Add(
new UILanguage
{
Culture = "es",
SpecCulture = "es-ES",
EnglishName = "Spanish"
});
CollectionViewSource cvs = new CollectionViewSource
{
Source = languages
};
cmbLanguages.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = cvs });
Markup
<ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,1,0,0"
Name="cmbLanguages" VerticalAlignment="Top" Width="207"
ItemsSource="{Binding Source={StaticResource UILanguagesViewSource}}"
/>
Please, note that
but this
Set SelectedItem of WPF ComboBox
and this
WPF combobox binding from code behind
doesn’t help at all.
Any clue?
Thank you!
You’d need to set the SelectedValue property to the specific instance of your language. This could be done via something like: