c# - Add New Usercontrol On button Click Command In WPF MVVM -


hi trying display usercontrol dynamically not working ...please me improve code . in cunstructor of mainwindowviewmodel tried set initial property of contentcontrol. thank in advance

<window x:class="wpfapplication1.mainwindow"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:vm="clr-namespace:wpfapplication1.viewmodel"             xmlns:view="clr-namespace:wpfapplication1.view"             title="mainwindow" height="350" width="525">         <window.resources>             <datatemplate datatype="{x:type vm:firstcontrolviewmodel}">                 <view:firstcontrol></view:firstcontrol>             </datatemplate>             <datatemplate datatype="{x:type vm:secondcontrolviewmodel}">                 <view:secondcontrol></view:secondcontrol>             </datatemplate>          </window.resources>         <grid>             <contentcontrol content="{binding loadedcontrol}" />         </grid>     </window> 

view model code :-

    public class mainviewmodel: inotifypropertychanged             {        public mainviewmodel()        {            loadedcontrol = "firstcontrolviewmodel";// here setting property                                                      //but not working        }         private string _loadedcontrol;         public string loadedcontrol        {            { return _loadedcontrol; }            set { _loadedcontrol = value;             notifypropertychanged("loadedcontrol");             }        } 

you need set loadedcontrol instance of viewmodel type, not string!

public mainviewmodel() {    loadedcontrol = new firstcontrolviewmodel(); }  private viewmodelbase _loadedcontrol;  public viewmodelbase loadedcontrol {     { return _loadedcontrol; }     set { _loadedcontrol = value;           notifypropertychanged("loadedcontrol");     } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -