How to attach behavior in code behind (Silverlight 3)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值