wpf - MVVM: Populate a view (with listbox) when an item from another view (with listbox) is selected -
i have list of topics, , each topic there examples. want able when topic selected populate view examples selected topic. i'm still learning mvvm not sure how implement such.
edit: attempting have 2 separate views (1 topics , 1 examples), , when topic selected 'dynamically' populate collection of examples , show them in listbox of examples' view.
this have far, idea pass viewmodel of example topicviewmodel, unsure how if both views set in xaml of main window:
public topicviewmodel selectedtopic { { return _selectedtopic; } set { _selectedtopic = value; onpropertychanged("selectedtopic"); //refresh list of exercises if (_exampleviewmodel != null) { _exampleviewmodel.refreshexercises(_selectedtopic.id); } } }
let's assume have this:
class topic { public observablecollection<example> examples { get; private set; } }
and in viewmodel somewhere this:
public observablecollection<topic> topics { get; private set; }
then binding in topics listbox might this:
<listbox x:name="topicslistbox" itemssource="{binding topics}" />
you bind examples
of listbox's selected item this:
<listbox x:name="exampleslistbox" itemssource="{binding selecteditem.examples, elementname=topicslistbox}"/>
Comments
Post a Comment