MVVM模式的View与ViewModel的三大通讯方式:Binding Data(实现数据的传递)、Command(实现操作的调用)和Attached
Behavior(实现控件加载过程中的操作)。
Behavior(实现控件加载过程中的操作)。
下面通过一个实例实现MVVM模式的Attached Behavior通讯,Attached Behavior与Command的区别就是,Attached Behavior在控件的加载过程中完成它的任务。
下面看一下实例的运行效果:
![](http://writeblog.csdn.net/userfiles/2011/6/19145226031250.jpg)
(1)MainPage.xaml页面代码,View层
< phone:PhoneApplicationPage
x:Class ="AttachedBehaviorDemo.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local ="clr-namespace:AttachedBehaviorDemo"
mc:Ignorable ="d" d:DesignWidth ="480" d:DesignHeight ="768"
FontFamily =" {StaticResource PhoneFontFamilyNormal} "
FontSize =" {StaticResource PhoneFontSizeNormal} "
Foreground =" {StaticResource PhoneForegroundBrush} "
SupportedOrientations ="Portrait" Orientation ="Portrait"
shell:SystemTray.IsVisible ="True" >
< Grid x:Name ="LayoutRoot" Background ="Transparent" >
< Grid.RowDefinitions >
< RowDefinition Height ="Auto" />
< RowDefinition Height ="*" />
</ Grid.RowDefinitions >
< StackPanel x:Name ="TitlePanel" Grid.Row ="0" Margin ="12,17,0,28" >
< TextBlock x:Name ="ApplicationTitle" Text ="MY APPLICATION" Style =" {StaticResource PhoneTextNormalStyle} " />
< TextBlock x:Name ="PageTitle" Text ="AttachedBehavior" Margin ="9,-7,0,0" Style =" {StaticResource PhoneTextTitle1Style} " />
</ StackPanel >
< Grid x:Name ="ContentPanel" Grid.Row ="1" Margin ="12,0,12,0" >
< TextBlock FontSize ="50" Text ="金色 Gold" local:Behavior.Brush ="Gold" Margin ="0,6,0,504" />
< TextBlock FontSize ="50" Text ="绿色 Green" local:Behavior.Brush ="Green" Margin ="0,94,0,416" />
< TextBlock FontSize ="50" Text ="蓝色 Blue" local:Behavior.Brush ="Blue" Margin ="0,184,0,339" />
< TextBlock FontSize ="50" Text ="橙色 Orange" local:Behavior.Brush ="Orange" Margin ="-3,274,3,243" />
< TextBlock FontSize ="50" Text ="紫色 Purple" local:Behavior.Brush ="Purple" Margin ="0,386,0,133" />
< TextBlock FontSize ="50" Text ="橄榄色 Olive" local:Behavior.Brush ="Olive" Margin ="0,497,0,0" Height ="110" VerticalAlignment ="Top" />
</ Grid >
</ Grid >
</ phone:PhoneApplicationPage >
(2)
Behavior.cs
类代码,处理附加的属性和事件。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace AttachedBehaviorDemo
{
static public class Behavior
{
// 注册一个附加属性BrushProperty,在XAML中名字为Brush,是Brush类型,在Hover类中,PropertyMetadata初始化元数据
public static readonly DependencyProperty BrushProperty = DependencyProperty.RegisterAttached(
" Brush " ,
typeof (Brush),
typeof (Behavior),
new PropertyMetadata ( null , new PropertyChangedCallback(OnHoverBrushChanged)));
/// <summary>
/// 获取Brush的属性值
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Brush GetBrush(DependencyObject obj)
{
return (Brush)obj.GetValue(BrushProperty);
}
/// <summary>
/// 设置属性的值
/// </summary>
/// <param name="obj"></param>
/// <param name="value"></param>
public static void SetBrush(DependencyObject obj, Brush value)
{
obj.SetValue(BrushProperty, value);
}
/// <summary>
/// 属性初始化
/// </summary>
/// <param name="obj"></param>
/// <param name="args"></param>
private static void OnHoverBrushChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
// 获取属性所在的TextBlock控件
TextBlock control = obj as TextBlock;
// 注册控件的事件
if (control != null )
{
// 注册鼠标进入事件
control.MouseEnter += new MouseEventHandler(OnControlEnter);
// 注册鼠标离开事件
control.MouseLeave += new MouseEventHandler(OnControlLeave);
}
}
/// <summary>
/// 鼠标进入事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void OnControlEnter( object sender, MouseEventArgs e)
{
// 获取当前的TextBlock控件
TextBlock control = (TextBlock)e.OriginalSource;
// 设置控件的前景颜色为红色
control.Foreground = new SolidColorBrush(Colors.Red);
}
/// <summary>
/// 鼠标离开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void OnControlLeave( object sender, MouseEventArgs e)
{
// 获取当前的TextBlock控件
TextBlock control = (TextBlock)e.OriginalSource;
// 设置控件的前景颜色为当前控件的Brush属性的值
control.Foreground = GetBrush(control);
}
}
}