wpf - Non XAML way to databind to collection/dictionary -


i have requirement dynamically load diagram (xaml includes custom usercontrol, screwcontrol).

i looking @ ways bind dependency property (status) of screwcontrol viewmodel class. challenge diagram contain varied number of screwcontrols, each of needed data bound dictionary or collection in viewmodel.

i have solved first challenge of loading xaml dynamically using xamlreader not sure how bind screwcontrols collection/dictionary viewmodel.

view model class

public class assemblyviewmodel {     public dictionary<int, screwstatus> screws { get; set; }     public frameworkelement image { get; set; }      ... }  

view class

public partial class assemblyview : usercontrol {     private assemblyviewmodel viewmodel;      public assemblyview(assemblyviewmodel viewmodel)     {         initializecomponent();         datacontext = this.viewmodel = viewmodel;          loaded += onloaded;     }      public frameworkelement xamlcontent     {         { return (frameworkelement)getvalue(xamlcontentproperty); }         set { setvalue(xamlcontentproperty, value); }     }      // using dependencyproperty backing store content.  enables animation, styling, binding, etc...     public static readonly dependencyproperty xamlcontentproperty =         dependencyproperty.register("xamlcontent", typeof(frameworkelement), typeof(assemblyview), new propertymetadata(null, onxamlchanged));      private static void onxamlchanged(dependencyobject dependencyobject, dependencypropertychangedeventargs args)     {         var view = dependencyobject assemblyview;         var element = args.newvalue frameworkelement;         if (null != element)         {             view.contentcontrol.children.removerange(0, 1);             view.contentcontrol.children.add(element);              var screws = element.findchildren<screw>();              try             {                 foreach (var screw in screws)                 {                     // screwcontrol has name properties "screw_1", 1 used key of dictionary , status value                     var parts = screw.name.split(new char[] { '_' }, stringsplitoptions.removeemptyentries);                     if (2 == parts.length)                     {                         bindingoperations.setbinding(                             screw,                             screw.screwstatusproperty,                             new binding(/*not sure goes here*/)                             {                                 source = view.viewmodel.screws,                             });                     }                 }             }             catch (exception e)             {                 throw;             }         }     }      private void onloaded(object sender, routedeventargs e)     {          // works , loads diagram          bindingoperations.setbinding(             this,             xamlcontentproperty,             new binding("image")             {                 source = this.viewmodel             });     } 

you there. should work:

foreach (var screw in screws) {     // screwcontrol has name properties "screw_1", 1 used key of dictionary , status value     var parts = screw.name.split(new char[] { '_' }, stringsplitoptions.removeemptyentries);     if (2 == parts.length)     {         bindingoperations.setbinding(             screw,             screw.screwstatusproperty,             new binding() // no need path we're going bind directly source value              {                 // source of databinding value of screws dictionary keyed screw id (parsed screw name)                 source = view.viewmodel.screws[(int)parts[1]],             });     } } 

although might more wpf'y screw controls loaded xaml public property , bind itemssource of itemscontrol. set itemtemplate contentcontrol content property bound screwstatus , contenttemplate bound current item (i.e. screw user control loaded xaml) provide more natural way bind view , model together.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -