c# - Bind Combo Box based on another Combo Box's selected item - MVVM WPF -


i have combo box populated artist names , need bind once artist selected. these set follows in view:

<combobox height="23" horizontalalignment="left" margin="65,81,0,0" name="combobox1" itemssource="{binding artists}" selecteditem="{binding selectedartist}" verticalalignment="top" width="120" />     <combobox height="23" horizontalalignment="left" margin="65,115,0,0" name="combobox2" verticalalignment="top" itemssource="{binding albums}" selecteditem="{binding selectedalbums}" width="120" /> 

in viewmodel have following:

private void initialiseartists()     {         musicdataclassesdatacontext dataclasses = new musicdataclassesdatacontext();          artistlist = (from m in dataclasses.tblartists select m.artistname).tolist();     }      public list<string> artists     {                 {             return this.artistlist;         }     }      public string selectedartist     {         set         {             this.selectedartist = value;             initialisealbums();         }     }      private void initialisealbums()     {         if (selectedartist != null)         {             musicdataclassesdatacontext dataclasses = new musicdataclassesdatacontext();              var getartist = dataclasses.tblartists.firstordefault(band => band.artistname == selectedartist);             albumlist = (from album in dataclasses.tblalbums                          album.artistid == getartist.artistid                          select album.albumname).tolist();              //dataclasses.tblalbums.selectmany(album => album.artistid == getartist.artistid).tolist();         }     }      public list<string> albums     {         set         {             initialisealbums();         }                 {             return this.albumlist;         }     } 

i assuming selected item event populate second combo box mistaken.

you have notify view albums property changed. that's why have implement inotifypropertychanged interface , call propertychanged event after building albums list.

for example

public class musics : inotifypropertychanged {    public event propertychangedeventhandler propertychanged;     private void onpropertychanged(string propertyname)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(propertyname));     }     private void initialisealbums()    {        if (selectedartist != null)        {             //your code             onpropertychanged("albums");        }     } } 

its workaround use observablecollection<> instead of list<>.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -