Behaviors are a new feature in Blend/Silverlight 3 and WPF. They allow designers to handle user interactions in XAML, and they also provide a clean division of concerns. Unfortunately, in the Blend 3 Preview, there aren't any behaviors included. This guide shows you how to write a behavior from scratch and also gives a look at a great new behavior written by Laurent Bugnion.
public MainPage()
{
InitializeComponent();
OurButton.Click += (s, e) =>
{
//message box
};
}
public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
{
}
public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
{
protected override void Invoke(object parameter)
{
HtmlPage.Window.Alert(DisplayText);
}
}
public class PopupBehavior : Microsoft.Expression.Interactivity.TargetedTriggerAction<FrameworkElement>
{
public static readonly DependencyProperty DisplayTextProperty =
DependencyProperty.Register("DisplayText", typeof(string), typeof(PopupBehavior), null);
[Category("DisplayText")]
public string DisplayText
{
get { return (string)GetValue(DisplayTextProperty); }
set { SetValue(DisplayTextProperty, value); }
}
protected override void Invoke(object parameter)
{
HtmlPage.Window.Alert(DisplayText);
}
}
Jose is a fellow Silverlight MVP and gives a great overview of Behaviors in Blend 3.
This is a talk that covers Behaviors. There are some surprising things that Behaviors can accomplish. There's a link from here to some sample Behaviors that can be downloaded.
This is a great overview (no code) of the thinking behind Behaviors.
Laurent has implemented a WPF Magnifying Behavior. In this post, he provides the source and a sample application.