wpf - Binding datacontext viewmodel to usercontrol view when using Mvvm -
i need binding viewmodel 2 usercontrol views have created.
have created reservation window should contain these 2 usercontrols.
what got:
- reservationview (window)
- reservationlistview (usercontrol)
- reservationdetailview (usercontrol)
reservationview (window) class
public partial class reservationview : window { public reservationview() { initializecomponent(); //datacontext = null; } }
reservationlistview (usercontrol) class:
public partial class reservationlistview : usercontrol, iviewreservationlistviewmodel { public reservationlistview(iviewreservationlistviewmodel viewmodel) { initializecomponent(); datacontext = viewmodel; } }
the iviewreservationlistviewmodel empty interface makes contact between usercontrol view , viewmodel.
reservationlistviewmodel
public class reservationlistviewmodel : inotifypropertychanged, iviewreservationlistviewmodel { public reservationlistviewmodel() { } }
when try set local:"usercontrol" in xaml inside reservationview, following error:
the type "reservationlistview" not include accessible constructors.
how should link 2 usercontrol respective viewmodels when use mvvm?
you have mainviewmodel like
class mainviewmodel { public reservationlistviewmodel reservationlistviewmodel { get; set; } public reservationdetailviewmodel reservationdetailviewmodel { get; set; } }
and use in xaml like
<window.datacontext> <local:mainviewmodel/> </window.datacontext> <local:reservationlistview datacontext="{binding reservationlistviewmodel}"/> <local:reservationdetailview datacontext="{binding reservationdetailviewmodel}"/>
your usercontrols wouldn't need constructors parameters.
Comments
Post a Comment