c# - Displaying a selected ComboBox Item in WPF -


i'm trying construct combobox display selected item. can combo box display options enough, getting selected item display not working. selected item refers value being pulled database.

i know set method working because can store user's choice. however, cannot seem combobox populate selected item when grab database.

i've been frustrated on few weeks now, , feel mistake simple.

scenario:

i have animal model

public class animalmodel {     public string animalname { get; set; }     ... } 

i have animalviewmodel each animal:

public class animalviewmodel: bindablebase {     private animalviewmodel _animal;      public animalviewmodel(animalmodel animal)     {         _animal = animal;     }      public string animalname     {         { return _animal.name; }         set         {             if (value != this._animal.name)             {                 this._animal.name = value;                 onpropertychanged("animalname");             }         }     }     ... } 

i have observablecollection of animalviewmodel objects:

public class templateviewmodel : bindablebase {     private observablecollection<animalviewmodel> _animals;     public templateviewmodel(...)     {         _animal = methodreturnsanobservablecollectionofanimalviewmodels();     }      public observablecollection<animalviewmodel> animals     {         { return _animals; }         set         {             if (value != this._animals)             {                 this._animals = value;                 onpropertychanged("animals");             }         }     } } 

with in place, can display list of animalnames in combobox:

<combobox itemssource="{binding animals}"            displaymemberpath="animalname"            iseditable="true"            isreadonly="true"            text="--- select animal ---"/> 

i bind selecteditem

public class templateviewmodel {     ...     private animalviewmodel _selectedanimal;     public templateviewmodel(myobject, ...)     {         ...         _selectedanimal = new animalviewmodel(new animalmodel() { animalname = myobject.animalname });     }     ...     public animalviewmodel selectedanimal     {         { return _selectedanimal; }         set         {             if (value != _selectedanimal)             {                 _selectedanimal = value;                 animalname = value.animalname;                 onpropertychanged("selectedanimal");             }         }     } } 

so now, have:

<combobox itemssource="{binding animals}"            displaymemberpath="animalname"            selecteditem={binding selectedanimal}            selectedvaluepath="animalname"            iseditable="true" isreadonly="true"            text="--- select animal ---"/> 

unfortunately, not populate combobox animal. pulls default choice select animal options populated. does set item correctly.

any appreciated.

you need access actual reference animal in question.

in codebehind create animal looks animal in observablecollection, not animal reference.

change (after animals loaded)

 _selectedanimal = new animalviewmodel(...); 

to (only use property accessor, not backing store variable; change event fires, btw)

selectedanimal = animals[0]; 

test app (formatting grid row/column removed)

<label>selecteditem</label> <textblock  text="{binding selecteditem, elementname=cbmain}"/> <label >selectedvalue</label> <textblock text="{binding selectedvalue, elementname=cbmain}"/> <button  click="changeselectedvalue">set selected value</button> <combobox name="cbmain"             itemssource="{binding ships}"             selecteditem="{binding ships[0]}"             selectedvaluepath="name"             text="select ship"/> 

result on load of screen:

enter image description here


Comments