微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐