I attached behavior (Behavior in Blend SDK) to a UIElement as I generated it in run time. I also had two buttons to trigger Detach() and Attach() – basically to enable or disable the behavior.
The problem is: after I Detach() the behavior, Attach() failed to restore the behavior to the dependency object, which then remained behavior-less:
// Declare the dependency object and behavior at class scope
MyControl c = new MyControl();
MyBehavior b = new MyBehavior();
// Function that generate UI Controls with behavior attached
b.Attach(c);
// Function that detach behavior from dependency object
b.Detach();
// Function that re-attach behavior to the same object after it has been detached
b.Attach(c); // <-- Do not see the behavior...
Why is the behavior not re-attaching? And is there a solution or work-around to switch on or off a behavior?
It seems the problem is specific of your behavior’s logic. In the following test the behavior reattaches without problems: