The API for this is not so obvious to do in code, use the following:
var behaviorCollection = System.Windows.Interactivity.Interaction .GetBehaviors(controlToAddBehaviorTo); behaviorCollection.Add(new MyBehavior());
where, obviously, controlToAddBehaviorTo is a "control that does not yet have one."
Same thing in xaml:
<TextBox x:Name="controlToAddBehaviorTo"> <i:Interaction.Behaviors> <local:MyBehavior/> </i:Interaction.Behaviors> </TextBox>
with import for xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" interactivity namespace xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
source share