c# - WPF combobox update other combobox based on selection of first one -
my wpf application has 2 comboboxes 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 code behind 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 updated langauges combobox based on project selection.
based on project selection able langauges project database, how set in langauge combobox.
langaugecombobox.selectedvalue = dt.rows[0][0].tostring(); not working.
and suggestion/help?
i advice start migrating events binding , change notifications. more wpf way.
so code 2 comboboxes become this, removed selectionchanged event handler , i'll remove 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 class following properties, instance of class goes datacontext view
public observablecollection<project> projects { { return (observablecollection<project>)getvalue(projectsproperty); } set { setvalue(projectsproperty, value); } } // using dependencyproperty backing store projects. 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 { { return (observablecollection<language>)getvalue(languagesproperty); } set { setvalue(languagesproperty, value); } } // using dependencyproperty backing store languages. enables animation, styling, binding, etc... public static readonly dependencyproperty languagesproperty = dependencyproperty.register("languages", typeof(observablecollection<language>), typeof(viewmodel), new propertymetadata(null)); public project selectedproject { { return (project)getvalue(selectedprojectproperty); } set { setvalue(selectedprojectproperty, value); } } // using dependencyproperty backing store selectedproject. enables animation, styling, binding, etc... public static readonly dependencyproperty selectedprojectproperty = dependencyproperty.register("selectedproject", typeof(project), typeof(viewmodel), new propertymetadata(null, onselectedprojectchanged)); public language selectedlanguage { { return (language)getvalue(selectedlanguageproperty); } set { setvalue(selectedlanguageproperty, value); } } // using dependencyproperty backing store selectedlanguage. enables animation, styling, binding, etc... public static readonly dependencyproperty selectedlanguageproperty = dependencyproperty.register("selectedlanguage", typeof(language), typeof(viewmodel), new propertymetadata(null));
then property change handler
private static void onselectedprojectchanged(dependencyobject d, dependencypropertychangedeventargs e) { view v = d view; v.selectedlanguage = //your logic here eg. v.selectedproject.language; }
so binding in place language combo box update selected item based on value set selectedlanguage.
Comments
Post a Comment