private static void AttachBehaviors(FrameworkElement frameworkElement, BehaviorCollection behaviors) { BindingOperations.SetBinding(behaviors, FrameworkElement.DataContextProperty, new Binding("DataContext") { Source = frameworkElement, }); foreach (var behavior in behaviors) { EventInfo ei = frameworkElement.GetType().GetEvent(behavior.EventName); if (ei == null) { return; } Behavior preventAccessToModifiedClosureBehavior = behavior; ei.AddEventHandler(frameworkElement, new RoutedEventHandler((x, y) => { Behavior b = preventAccessToModifiedClosureBehavior; b.Command.Execute(b.CommandParameter); })); } }
public static void SetBehaviors(DependencyObject obj, BehaviorCollection value) { obj.SetValue(BehaviorsProperty, value); }