WPF / Silverlight: how to create a BehaviorCollection for a control that does not already have this?

How to create a BehaviorCollection on a control that does not already have this?

+4
source share
1 answer

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"

+5
source

Source: https://habr.com/ru/post/1338926/


All Articles