个人网站的设计与开发,网站更新怎么做,柳市网站优化,天津seo排名费用利刃 MVVMLight 3#xff1a;双向数据绑定 原文:利刃 MVVMLight 3#xff1a;双向数据绑定上篇我们已经了解了MVVM的框架结构和运行原理。这里我们来看一下伟大的双向数据绑定。说到双向绑定#xff0c;大家比较熟悉的应该就是AngularJS了#xff0c;几乎所有的AngularJS 系… 利刃 MVVMLight 3双向数据绑定 原文:利刃 MVVMLight 3双向数据绑定上篇我们已经了解了MVVM的框架结构和运行原理。这里我们来看一下伟大的双向数据绑定。 说到双向绑定大家比较熟悉的应该就是AngularJS了几乎所有的AngularJS 系列教程的开篇几章都要涉及到真的是很好用。 表达的效果很简单就是在界面的操作对数据模型的修改能实时反映到数据而数据的变更能实时展现到界面。即视图数据模型ViewModel和视图View之间的双向绑定和触发。 我们来操作一个试试看 第一步先写一个Model里面包含我们需要的数据信息代码如下 1 /// summary2 /// 用户信息3 /// /summary4 public class UserInfoModel : ObservableObject5 {6 private String userName;7 /// summary8 /// 用户名称9 /// /summary
10 public String UserName
11 {
12 get { return userName; }
13 set { userName value; RaisePropertyChanged(()UserName); }
14 }
15
16 private Int64 userPhone;
17 /// summary
18 /// 用户电话
19 /// /summary
20 public Int64 UserPhone
21 {
22 get { return userPhone; }
23 set { userPhone value; RaisePropertyChanged(() UserPhone); }
24 }
25
26 private Int32 userSex;
27 /// summary
28 /// 用户性别
29 /// /summary
30 public Int32 UserSex
31 {
32 get { return userSex; }
33 set { userSex value; RaisePropertyChanged(()UserSex); }
34 }
35
36 private String userAdd;
37 /// summary
38 /// 用户地址
39 /// /summary
40 public String UserAdd
41 {
42 get { return userAdd; }
43 set { userAdd value; RaisePropertyChanged(() UserAdd); }
44 }
45 } 第二步写一个ViewModel包含了View所需要的命令和属性 1 public class BothWayBindViewModel:ViewModelBase2 {3 public BothWayBindViewModel()4 {5 UserInfo new UserInfoModel();6 }7 8 #region 属性9
10 private UserInfoModel userInfo;
11 /// summary
12 /// 用户信息
13 /// /summary
14 public UserInfoModel UserInfo
15 {
16 get { return userInfo; }
17 set { userInfo value; RaisePropertyChanged(() UserInfo); }
18 }
19
20 #endregion
21
22 #region 命令
23 #endregion
24 } 第三步在ViewModelLocator中注册我们写好的ViewModelSimpleIoc.Default.RegisterBothWayBindViewModel(); 1 /*2 In App.xaml:3 Application.Resources4 vm:ViewModelLocator xmlns:vmclr-namespace:MVVMLightDemo5 x:KeyLocator /6 /Application.Resources7 8 In the View:9 DataContext{Binding Source{StaticResource Locator}, PathViewModelName}
10
11 You can also use Blend to do all this with the tools support.
12 See http://www.galasoft.ch/mvvm
13 */
14
15 using GalaSoft.MvvmLight;
16 using GalaSoft.MvvmLight.Ioc;
17 using Microsoft.Practices.ServiceLocation;
18
19 namespace MVVMLightDemo.ViewModel
20 {
21 /// summary
22 /// This class contains static references to all the view models in the
23 /// application and provides an entry point for the bindings.
24 /// /summary
25 public class ViewModelLocator
26 {
27 /// summary
28 /// Initializes a new instance of the ViewModelLocator class.
29 /// /summary
30 public ViewModelLocator()
31 {
32 ServiceLocator.SetLocatorProvider(() SimpleIoc.Default);
33
34 #region Code Example
35 ////if (ViewModelBase.IsInDesignModeStatic)
36 ////{
37 //// // Create design time view services and models
38 //// SimpleIoc.Default.RegisterIDataService, DesignDataService();
39 ////}
40 ////else
41 ////{
42 //// // Create run time view services and models
43 //// SimpleIoc.Default.RegisterIDataService, DataService();
44 ////}
45 #endregion
46
47 SimpleIoc.Default.RegisterMainViewModel();
48 SimpleIoc.Default.RegisterWelcomeViewModel();
49 SimpleIoc.Default.RegisterBothWayBindViewModel();
50 }
51
52 #region 实例化
53 public MainViewModel Main
54 {
55 get
56 {
57 return ServiceLocator.Current.GetInstanceMainViewModel();
58 }
59 }
60
61 public WelcomeViewModel Welcome
62 {
63 get
64 {
65 return ServiceLocator.Current.GetInstanceWelcomeViewModel();
66 }
67 }
68
69 public BothWayBindViewModel BothWayBind
70 {
71 get
72 {
73 return ServiceLocator.Current.GetInstanceBothWayBindViewModel();
74 }
75 }
76
77 #endregion
78
79 public static void Cleanup()
80 {
81 // TODO Clear the ViewModels
82 }
83 }
84 } 第四步编写View注意标红的代码 1 Window x:ClassMVVMLightDemo.View.BothWayBindView2 xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation3 xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml4 DataContext{Binding Source{StaticResource Locator},PathBothWayBind}5 TitleBothWayBindView Height300 Width3006 Grid7 StackPanel OrientationVertical Margin10,10,0,08 StackPanel OrientationHorizontal 9 TextBlock Text请输入姓名 /TextBlock
10 TextBox Text{Binding UserInfo.UserName,UpdateSourceTriggerPropertyChanged,ModeTwoWay} Width200 /TextBox
11 /StackPanel
12
13 StackPanel Margin0,10,0,0 OrientationHorizontal
14 TextBlock TextHello /TextBlock
15 TextBlock Text{Binding UserInfo.UserName} /TextBlock
16 /StackPanel
17
18 StackPanel HorizontalAlignmentCenter VerticalAlignmentCenter OrientationHorizontal
19 /StackPanel
20
21 /StackPanel
22 /Grid
23 /Window 效果如图所示当修改输入框的内容的时候对应绑定数据相应改变并触发对UI的修改所以下面那行文字也相应改变改变。 前面我们已经了解到了RaisePropertyChanged的作用是当数据源改变的时候会触发PropertyChanged事件达到通知UI更改的目的ViewModel View。 那View上的变化要怎么通知到数据源呢 View中文本框绑定内容如下{Binding UserInfo.UserName,UpdateSourceTriggerPropertyChanged,ModeTwoWay} 大家会看到多了两个属性一个是UpdateSourceTrigger一个是Mode属性。 UpdateSourceTrigger的作用是 当做何种改变的时候通知数据源我们做了改变。 枚举类型效果Default默认值默认为LostFocuseExplicit当应用程序调用 UpdateSource 方法时生效LostFocus失去焦点的时候触发PropertyChanged数据属性改变的时候触发 这边我们直接使用 PropertyChanged当UI数据改变的时候我们再通知到数据源去做修改。 还有一个属性就是Mode他有五个参数 枚举类型效果OneWay源发生变化数据就会从源流向目标OneTime绑定会将数据从源发送到目标但是仅当启动了应用程序或 DataContext 发生更改时才会如此操作因此它不会侦听源中的更改通知。OneWayToSource绑定会将数据从目标发送到源TwoWay绑定会将源数据发送到目标但如果目标属性的值发生变化则会将它们发回给源Default绑定的模式根据实际情况来定如果是可编辑的就是TwoWay,只读的就是OneWay 这边明显有很多种选择明确一点的是我们是想把View上的变化同步到ViewModelTarget Source所以使用OneWayToSource、TwoWay、Default或者不写都可以。 严谨点应该使用OneWayToSource。因为是文本框属于可以编辑控件所以 Default指向的是TwoWay。 下面还有一个TextBlock仅仅用于显示的所以不需要目标对源的修改无需指定就默认是OneWay当源改变的时候会通知它进行修改。 点击下载代码 转载请标明出处谢谢 posted on 2018-06-01 10:32 NET未来之路 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/lonelyxmas/p/9120505.html