c# - WPF Dynamically add single control through binding using MVVM -
okay, did bunch of searching around , either can't seem wrap head around right question, or isn't done...
basically have modular application contains various data-driven 'applets', can potentially change , dynamically added in based upon permissions have been provided user. i've built application in flex, , moving wpf through mvvm because it's million times easier code , maintain.
now, there 2 issues i'm trying figure out:
issue #1
i need able dynamically add single control through binding. i've built bindings using data templates on itemscontrols hook list of objects in viewmodel, , works wonderfully, system seems little hacky when know there ever 1 control gets me now.
here code show why seems hacky me:
xaml
<itemscontrol itemssource="{binding applicationlist}"> <itemscontrol.itemtemplate> <datatemplate> <ctrl:portalapplicationcontrol /> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> viewmodel
/// <summary> /// list contain active application binding itemscontrol in xaml /// </summary> public list<models.application> applicationlist { { return (list<models.application>)getvalue(applicationlistproperty); } set { setvalue(applicationlistproperty, value); } } public static readonly dependencyproperty applicationlistproperty = dependencyproperty.register("applicationlist", typeof(list<models.application>), typeof(applicationvm)); /// <summary> /// active application /// </summary> public models.application application { { return (models.application)getvalue(applicationproperty); } set { setvalue(applicationproperty, value); applicationlist = new list<models.application>() { application }; } } public static readonly dependencyproperty applicationproperty = dependencyproperty.register("application", typeof(models.application), typeof(applicationvm)); as can see, it's hacky in sense objects binding properties on application dependency property able call '{binding application.name}' itemscontrol has call '{binding applicationlist}' , setter application has instantiate new 'list' of length 1 set applicationlist object in turn....it works, it's not elegant , want know if there way of accomplishing task
issue #2
okay, part throws monkey wrench in entire structure above: if want different controls in data template based upon application object active? i.e. if application1 active, use application1view, if application2 active, use application2view...this seems going have work in code behind work...and strikes me big no no if i'm sticking mvvm structure
sorry being long winded, hope question makes sense , guys can offer...this putting project @ standstill until can resolve above issues
if there 1 control @ same time, use contentcontrol.
Comments
Post a Comment