コード例 #1
0
        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);
                }));
            }
        }
コード例 #2
0
 public static void SetBehaviors(DependencyObject obj, BehaviorCollection value)
 {
     obj.SetValue(BehaviorsProperty, value);
 }