For those of you who would like to add (attach) behavior to control in code behind, there is a simple solution.
Adding Behaviors
Let's say that we have xaml code like this one:
<TextBox x:Name="TextBoxInvoker" Text="TextBoxControl" >
<i:Interaction.Behaviors>
<behavior:TextBoxSimpleBehavior />
</i:Interaction.Behaviors>
</TextBox>
, where "i:" is a namespace for "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" and "behavior:" is a namespace for behavior's class "TextBoxSimpleBehavior", which can be simple class like this one (just adding typed letters to content):
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
/// Dummy sample behavior for TextBox control.
/// It add extra letter in the end of Text property.
/// <author>
/// Jacek Ciereszko
/// http://geekswithblogs.net/SilverBlog/
/// </author>
namespace TextBoxEnterBehavior
{
public class TextBoxSimpleBehavior : Behavior<TextBox>
{
public TextBoxSimpleBehavior() : base() { }
/// <summary>
/// Called after the Behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.KeyDown += new KeyEventHandler(AssociatedObject_KeyDown);
}
/// <summary>
/// Called after the Behavior is detached from an AssociatedObject.
/// </summary>
/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.KeyDown += new KeyEventHandler(AssociatedObject_KeyDown);
}
/// Simple operation on TextBox
void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
{
this.AssociatedObject.Text += e.Key;
}
}
}
So, to do the same operation in code behind, write:
// Adding behavior in code behind
TextBoxSimpleBehavior textBoxSimpleBehavior = new TextBoxSimpleBehavior();
System.Windows.Interactivity.Interaction.GetBehaviors(TextBoxInvoker).Add(textBoxSimpleBehavior);
Adding Triggers
In this example I will use my TargetedTriggerAction from previous post http://geekswithblogs.net/SilverBlog/archive/2009/09/21/behaviors-textbox-enter-button-invoke-targetedtriggeraction.aspx
In xaml I attached my TriggerAction using this code:
<Button x:Name="TargetedButton" Content="Targeted Button" />
<TextBox x:Name="TextBoxInvoker" Text="TextBox" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown" >
<behavior:TextBoxEnterButtonInvoke TargetName="TargetedButton" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
where "i:" is a namespace for "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" and "behavior:" is a namespace for behavior's code.
In code behind I will write this code:
// Adding TriggerAction in code behind
TextBoxEnterButtonInvoke textBoxEnterButtonInvoke = new TextBoxEnterButtonInvoke();
textBoxEnterButtonInvoke.TargetName = "TargetedButton";
System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("KeyDown");
eventTrigger.Actions.Add(textBoxEnterButtonInvoke);
System.Windows.Interactivity.Interaction.GetTriggers(TextBoxInvoker).Add(eventTrigger);
Done! That's all we need to attach behaviors in code behind.