Tuesday, June 14, 2016

Introduction of Behaviors

Behaviors encapsulate the pieces of functionality into a reusable component, so that can attach to a element.


To be more clear - Suppose we have a Text box, and the requirement is when there is data in the text box the background will be green other wise white.

for simply without behaviour we can achieve this by writing some event and code for this, but if the same this is needed for many Text boxes for different UI, we need to write the code again and again. (It is a very simple example but real scenario can be more complex)
Behaviour just wrap this functionality and can be attach with element if needed with element.

Behaviour introduces in Expression Blend 3 (System.Windows.interactivity.dll)

Benefit -
Behavior is to provide additional functionalities and extensibility to controls without writing code in the code-behind.

Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from code behind to behaviours.

Behaviour is reusable component, write once and use later as many time is required.


Implementation
TO create a Behavior  Just drive your class with Behavior<T>(Behavior Generic class) and there is a method OnAttached override it.

Behavior is the base class for providing attachable state and commands to an object. The types the Behavior can be attached to can be controlled by the generic parameter. Override OnAttached() and OnDetaching() methods to hook and unhook any necessary handlers from the AssociatedObject.(Source MSDN)

public abstract class Behavior<T> : Behavior where T : DependencyObject

There is two important method OnAttached() and OnDetaching() in Behaviour class, this can be override to implement Behavior.


Behavior can be simple and with command.


E.g
Lets we have a situation where we need Textbox , if there is data the background turn to Green other wise it is Red.

Create a Behavior class TextChangeBehaviour.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace BehavoiurTest
{
    public class TextChangeBehaviour : Behavior<TextBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
           TextBox txtBox= AssociatedObject as TextBox;
           txtBox.TextChanged += txt_TextChanged;
           if (string.IsNullOrEmpty(txtBox.Text))
           {
               txtBox.Background = new SolidColorBrush(Colors.Green);
           }
           else
           {
               txtBox.Background = new SolidColorBrush(Colors.Red);
           }
           
        }

        void txt_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtBox = AssociatedObject as TextBox;
            txtBox.TextChanged += txt_TextChanged;

            if (string.IsNullOrEmpty(txtBox.Text))
           {
               txtBox.Background = new SolidColorBrush(Colors.Green);
           }
           else
           {
               txtBox.Background = new SolidColorBrush(Colors.Red);
           }
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            TextBox txtBox = AssociatedObject as TextBox;
            txtBox.TextChanged -= txt_TextChanged;
          
        }
    }
}


To attach this behaviour with Text box, suppose we have a window TextChangeBhehaviourTest.xaml

<Window x:Class="WpfApplication1.TextChangeBhehaviourTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:TxtChBhe="clr-namespace:BehavoiurTest"
        xmlns:e="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        Title="TextChangeBhehaviourTest" Height="300" Width="300">
    <Grid>
        <TextBox Width="150" Height="25">
            <e:Interaction.Behaviors>
                <TxtChBhe:TextChangeBehaviour></TxtChBhe:TextChangeBehaviour>
            </e:Interaction.Behaviors>
        </TextBox>
    </Grid>
</Window>



here we need to add assembly of Interactivity, and Behaviour.

No comments:

Post a Comment