silverlighter下MVVM模式中利用Behavior和TargetedTriggerAction实现文本框的一些特效
在silverlight一般开发模式中,给文本框添加一些事件是轻而易举的,然而MVVM开发模式中,想要给文本框添加一些事件并非那么容易,因为MVVM模式中,只有ICommand接口,而且也只有Button中才有Command属性,通过ViewModel可以将方法绑定到Button上面,却无法绑定到文本框和其他一些控件。、
Behavior的出现解决了这一难题,下面就来说一下具体的实现方法:
实例一:在用户登录窗口,用户点击Reset按钮后,让用户名输入框自动获取焦点。
首先要先将ViewModel绑定到我们的控件上面,我们一步一步来做,第一步先写Model,下面是Model的代码:
using System;
using System.Net;
using System.Runtime.Serialization;
using System.ComponentModel; namespace BookModel
{
[DataContract]
public class UserModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private string userName = String.Empty;
private string passWord = String.Empty; [DataMember]
public string UserName
{
get { return userName; }
set { userName = value; OnPropertyChanged("UserName"); }
} [DataMember]
public string PassWord
{
get { return passWord; }
set { passWord = value; OnPropertyChanged("PassWord"); }
} /// <summary>
/// Call the event PropertyChanged.
/// </summary>
/// <param name="PropertyName"></param>
public void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
}
写完了Model,下一步就是写ViewModel了,在ViewModel中引用Model的命名控件,下面是ViewModel的代码,例子比较简单,就不多解释了。
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 BookModel;
using System.Windows.Browser; namespace BookViewModel
{
public class VM_User
{
/// <summary>
/// In the Constructors create the instance of User.
/// </summary>
public VM_User()
{
user = new UserModel();
user.UserName = "Jack";
} public UserModel user { get; set; }
public LoginCommand Login
{
get { return new LoginCommand(); }
}
public ResteCommand Reset
{
get { return new ResteCommand(); }
}
} /// <summary>
/// Login Button's method
/// </summary>
public class LoginCommand:ICommand
{
public bool CanExecute(object parameter)
{
return true;
} public void Execute(object parameter)
{
VM_User VMUser = parameter as VM_User;
if (VMUser.user.UserName.Equals("admin") && VMUser.user.PassWord.Equals(""))
{
MessageBox.Show("Login success!", "System Info", MessageBoxButton.OK);
}
else
{
MessageBox.Show("Login failed!", "System Info", MessageBoxButton.OK);
}
} public event EventHandler CanExecuteChanged;
} /// <summary>
/// Reset buttom's method
/// </summary>
public class ResteCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
} public void Execute(object parameter)
{
VM_User VMUser = parameter as VM_User;
VMUser.user.UserName = "";
VMUser.user.PassWord = "";
}
public event EventHandler CanExecuteChanged;
}
}
这里就不连接数据库了,那样代码量太大,也不方便大家查看。这里只做一个简单的验证,至此MVVM中的M和VM就都建好了,下面写前端的显示层,添加两个TextBlock和两个TextBox,两个按钮。用作登录用,分别为用户名,密码,登录和重置。
下面是绑定代码:
<UserControl x:Class="BookLibrary.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BookViewModel;assembly=BookViewModel"
mc:Ignorable="d"
d:DesignHeight="" d:DesignWidth=""
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"> <UserControl.Resources>
<local:VM_User x:Key="User" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource User}}" Loaded="LayoutRoot_Loaded">
<telerik:RadBusyIndicator Name="BusyIndicator">
<Border>
<Border.Background>
<LinearGradientBrush EndPoint="0.5 1" StartPoint="0.5 0">
<GradientStop Color="#0369a9" Offset="" />
<GradientStop Color="#046daf" Offset="0.5" />
<GradientStop Color="#2fa2e5" Offset="" />
</LinearGradientBrush>
</Border.Background>
<Border VerticalAlignment="Center" HorizontalAlignment="Center">
<Border.Effect>
<DropShadowEffect BlurRadius="" Opacity="0.4"/>
</Border.Effect>
<Border.Background>
<RadialGradientBrush>
<GradientStop Color="#0B7AC1" Offset="" />
<GradientStop Color="#1182c7" Offset="" />
</RadialGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=""/>
<RowDefinition Height=""/>
<RowDefinition Height=""/>
<RowDefinition Height=""/>
<RowDefinition Height=""/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width=""/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left"
Grid.Row="" Grid.Column="" Margin="20,0,0,0" FontWeight="Bold"
Text="School System |" FontSize="" Foreground="White"/>
<TextBlock Grid.Row="" Grid.Column="" HorizontalAlignment="Left"
VerticalAlignment="Center" Text="User Login" FontSize=""
Foreground="#b4e6ec"/>
<Image Source="Images/LoginPicture.png" Grid.Row="" Grid.Column="" Grid.RowSpan="" />
<TextBlock FontSize="" Grid.Row="" Grid.Column="" Text="UserName:"
Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBlock FontSize="" Grid.Row="" Grid.Column="" Text="PassWord:"
Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox Name="txtName" Text="{Binding user.UserName,Mode=TwoWay}" HorizontalAlignment="Left" FontSize="" Grid.Row="" Grid.Column="" Width="" Height="" />
<PasswordBox Password="{Binding user.PassWord,Mode=TwoWay}" HorizontalAlignment="Left" Grid.Row="" Grid.Column="" Width="" Height="" />
<Button Command="{Binding Login}" CommandParameter="{Binding}" HorizontalAlignment="Left" Width="" Height="" Grid.Row="" Grid.Column="" Content="Login" Click="Button_Click_1" />
<Button Command="{Binding Reset}" CommandParameter="{Binding}" HorizontalAlignment="Left" Width="" Height="" Grid.Row="" Grid.Column="" Content="Reset" Click="Button_Click" />
<CheckBox HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" Grid.Row="" Grid.Column="" Content="Save State" Foreground="White" /> </Grid>
</Border>
</Border>
</telerik:RadBusyIndicator> </Grid>
</UserControl>
这个时候,就实现登录和重置功能了,但是重置的时候用户名文本框并不会获得焦点,下面我们来讲实现方法:
第一种方法:TargetedTriggerAction绑定
先写一个类,叫做TextBoxGetFocusBahavior,代码如下:
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; namespace BookLibrary
{
public class TextBoxGetFocusBahavior:TargetedTriggerAction<TextBox>
{
public void GotFocusAction()
{ }
protected override void Invoke(object parameter)
{
TextBox targetTextBox = Target;
targetTextBox.SelectAll();
}
}
}
然后在前台绑定,绑定方法如下:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<TextBox Height="" Name="textBox" HorizontalAlignment="Left" Margin="36,28,0,0" VerticalAlignment="Top" Width="">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<local:TextBoxGetFocusBahavior TargetName="textBox" />
</i:EventTrigger>
</i:Interaction.Triggers> </TextBox>
注意我标红的地方,就是要引入命名空间。
第二种方法Behavior方法,和上面基本一样,不过我感觉这种方法比较灵活,推荐使用这种方法。新加一个类,AutoSetFocusBehavior,代码如下:
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; namespace BookLibrary
{
public class AutoSetFocusBehavior:Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.TextChanged += OnTextChanged;
} public void OnTextChanged(object sender, EventArgs e)
{
if(AssociatedObject.Text.Equals(""))
AssociatedObject.Focus();
}
}
}
这里前端绑定方法为:
<TextBox Text="{Binding user.UserName}" x:Name="txtName" Height="" HorizontalAlignment="Left" Margin="36,76,0,0" VerticalAlignment="Top" Width="">
<i:Interaction.Behaviors>
<local:AutoSetFocusBehavior />
</i:Interaction.Behaviors>
</TextBox>
注意,同样要引入上面的命名空间。
其实,说了这么多,就是给TextBox加了个OnTextChanged事件,当内容被清空时,判断内容是否为空,为空则设置焦点。
希望这篇文章能给大家一点帮助。不足之处,还请赐教。
silverlighter下MVVM模式中利用Behavior和TargetedTriggerAction实现文本框的一些特效的更多相关文章
- [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦
[.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...
- Messenger在MVVM模式中的应用
Messenger在MVVM模式中的应用 Messenger在MVVM中应用的前提 我们知道在MVVM架构中,系统平台的Silverlight客户端界面开发和业务逻辑已经被分开,XAML是SL的主要部 ...
- 在MVVM模式中,按钮Click事件的绑定方法
在MVVM模式中,我们将Button的方法写到ViewModel中,然后绑定到前端界面.通常的做法是写一个类,继承ICommand接口,然而如果按钮比较多的话,就需要写很多的类,对于后期维护造成很大的 ...
- “Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置
这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题.比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一 ...
- WPF MVVM模式中,通过命令实现窗体拖动、跳转以及显隐控制
原文:WPF MVVM模式中,通过命令实现窗体拖动.跳转以及显隐控制 在WPF中使用MVVM模式,可以让我们的程序实现界面与功能的分离,方便开发,易于维护.但是,很多初学者会在使用MVVM的过程中遇到 ...
- WPF ContextMenu 在MVVM模式中绑定 Command及使用CommandParameter传参
原文:WPF ContextMenu 在MVVM模式中绑定 Command及使用CommandParameter传参 ContextMenu无论定义在.cs或.xaml文件中,都不继承父级的DataC ...
- js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符
js中对arry数组的各种操作小结 最近工作比较轻松,于是就花时间从头到尾的对js进行了详细的学习和复习,在看书的过程中,发现自己平时在做项目的过程中有很多地方想得不过全面,写的不够合理,所以说啊 ...
- Jquery获取下拉选择节点名称值赋给textbox文本框 获取 父节点的栏目名称编号
<label for="parentNode" style="float:left" >父级栏目:</label> <select ...
- HTML中让表单input等文本框为只读不可编辑但可以获取value值的方法;让文本域前面的内容显示在左上角,居中
HTML中让表单input等文本框为只读不可编辑的方法 有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使input text的内容,中国两个字不可以修改 有时候,我们希望 ...
随机推荐
- windows获取硬盘使用率等信息
#coding=utf8 import psutil cpu = {'user' : 0, 'system' : 0, 'idle' : 0, 'percent' : 0} mem = {'total ...
- .Net中的加密解密
返回博客列表 转 .Net中的加密解密 李朝强 发布时间: 2015/11/23 12:55 阅读: 33 收藏: 3 点赞: 0 评论: 0 在一些比较重要的应用场景中,通过网络传递数据需要进行加密 ...
- 1 Java线程的内存可见性
Java内存的可见性 可见性: 一个线程对共享变量的修改,能够及时被其它线程看到 共享变量: 如果一个变量在多个线程的工作内存中都存在副本,那么这个变量就是这几个线程的共享变量 Java内存模型(JM ...
- win7环境下安装运行gotour【转载整理】
转载请注明出处:http://www.cnblogs.com/Vulpers/p/5562586.html 最近尝试学习golang,在某个网站(真忘了)上发现gotour是一款灰常叼的教程& ...
- Unity3D安卓打包参数配置与兼容性的关系分析
前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...
- java设计模式之-----桥接模式
一.概念定义 将抽象部分和实现部分分离,使他们都可以独立的变化.(这句话有点抽象啊..) 在软件系统中,某种类型由于自身逻辑,具有多个维度的变化,如何利用面向对象的技术来使得该类型沿着多方向变化,而又 ...
- MySQL执行计划解读
Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...
- Word 2007 文档结构图混乱
Word 2007在编写大型文档时经常出现文档结构图混乱的情况,经过多番检索试验,得出结论: 绝对有效的临时性解决方案:在打开word的时候左下角会有提示word自动更新文档样式,按esc键取消,然后 ...
- JS实现回到顶部效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 微信分享接口SDK简介使用
很久没做移动端活动了,想不少人都参与过微信里面的活动,既然活动就少不了分享.那我就行一个前端的角度说说如何实现微信分享的吧. 对应微信分享,现在已有公共接口,微信的开发者文档SDK:https://m ...