Reputation: 539
My WPF application has 2 comboboxes as follows
<ComboBox Height="23" Name="ProjectNameComboBox" Width="215" ItemsSource="{Binding}" DisplayMemberPath="name" SelectionChanged="ProjectSelection_Changed" />
<ComboBox Height="25" Name="LangaugeComboBox" Width="108" ItemsSource="{Binding}" DisplayMemberPath="name" />
and from the code behind I am binding using database as
ProjectNameComboBox.DataContext = dbConnector.GetProjectNames();
LangaugeComboBox.DataContext = dbConnector.GetLanguages();
project combobox contains list of projects
langauges combobox contains list of langauges.
I want to updated the langauges combobox based on project selection.
based on project selection I am able to get the langauges for that project from database, but how to set it in langauge combobox.
LangaugeComboBox.SelectedValue = dt.Rows[0][0].ToString(); is not working.
and suggestion/help?
Upvotes: 0
Views: 1997
Reputation: 13669
I would advice you to start with migrating from events to binding and change notifications. That would be more WPF way.
so the code for two comboboxes become like this, I removed the SelectionChanged event handler and I'll also remove the Name unless required
<ComboBox Height="23" Width="215" ItemsSource="{Binding Projects}" DisplayMemberPath="name" SelectedItem="{Binding SelectedProject}" />
<ComboBox Height="25" Width="108" ItemsSource="{Binding Languages}" DisplayMemberPath="name" SelectedItem="{Binding SelectedLanguage}" />
then (instead of) code behind a class with following properties, then instance of this class goes as DataContext for the View
public ObservableCollection<Project> Projects
{
get { return (ObservableCollection<Project>)GetValue(ProjectsProperty); }
set { SetValue(ProjectsProperty, value); }
}
// Using a DependencyProperty as the backing store for Projects. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ProjectsProperty =
DependencyProperty.Register("Projects", typeof(ObservableCollection<Project>), typeof(ViewModel), new PropertyMetadata(null));
public ObservableCollection<Language> Languages
{
get { return (ObservableCollection<Language>)GetValue(LanguagesProperty); }
set { SetValue(LanguagesProperty, value); }
}
// Using a DependencyProperty as the backing store for Languages. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LanguagesProperty =
DependencyProperty.Register("Languages", typeof(ObservableCollection<Language>), typeof(ViewModel), new PropertyMetadata(null));
public Project SelectedProject
{
get { return (Project)GetValue(SelectedProjectProperty); }
set { SetValue(SelectedProjectProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedProject. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedProjectProperty =
DependencyProperty.Register("SelectedProject", typeof(Project), typeof(ViewModel), new PropertyMetadata(null, OnSelectedProjectChanged));
public Language SelectedLanguage
{
get { return (Language)GetValue(SelectedLanguageProperty); }
set { SetValue(SelectedLanguageProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedLanguage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedLanguageProperty =
DependencyProperty.Register("SelectedLanguage", typeof(Language), typeof(ViewModel), new PropertyMetadata(null));
then finally the property change handler
private static void OnSelectedProjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
View v = d as View;
v.SelectedLanguage = //your logic here eg. v.SelectedProject.Language;
}
so with the binding in place your language combo box will update the selected item based on the value you set to SelectedLanguage.
Upvotes: 1
Reputation: 3563
If your Language ComoboBox
is not bounded to a complex object and it is a string
collection, please try below code
LangaugeComboBox.SelectedItem = dt.Rows[0][0].ToString();
Upvotes: 0