wpf controls - How to add WPF Behavior in the code behind -
i trying find way add behavior in code, able add in xaml.
this how adding behavior in xaml grid, selecteditems dp in behavior , data bind view model selected items property. listening grid collection changed event , updating dp in turns notify view mode of selected items
/// <summary> /// dependency property selecteditems /// </summary> public static readonly dependencyproperty selecteditemsproperty = dependencyproperty.register("selecteditems", typeof(inotifycollectionchanged), typeof(multiselectbehavior), new propertymetadata(null)); associatedobject.selecteditems.collectionchanged += gridselecteditems_collectionchanged;
<i:interaction.behaviors> <behaviors:multiselectbehavior selecteditems="{binding selecteditems}"/> </i:interaction.behaviors>
what need create behavior in code behind. doing in constructor of window contains grid, not working, viewmodel selected items property not getting updated.
var multiselectbehavior = new multiselectbehaviorsingleton(); bindingoperations.setbinding(this.backupsgrid, multiselectbehavior.selecteditemsproperty, new binding() { source = this.datacontext, path = new propertypath("selecteditems"), mode = bindingmode.oneway }); interaction.getbehaviors(this.backupsgrid).add(multiselectbehavior);
try this:
var multiselectbehavior = new multiselectbehavior(); bindingoperations.setbinding(multiselectbehavior, multiselectbehavior.selecteditemsproperty, new binding("selecteditems")); interaction.getbehaviors(this.backupsgrid).add(multiselectbehavior);
Comments
Post a Comment