view-viewModel-model,模型介紹省略,就是創(chuàng)建類,添加字段封裝屬性。注:控件的綁定只能綁定到屬性上,不能綁定到字段上;
接下來就是代碼
1 <Window x:Class="WpfBing.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/說明:
xmlns:vm="clr-namespace:WpfBing"添加對命名空間的引用,主要是讓前臺頁面能夠?qū)ふ业絭iewmodel的命名空間;xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 添加wpf的命名控件引用,主要是用來提供使用該命名空間中的觸發(fā)器綁定命令該類的下載鏈接為:System.Windows.Interactivity<Grid.DataContext> <vm:ViewModel/> </Grid.DataContext> 數(shù)據(jù)源綁定,將該空間按的數(shù)據(jù)源綁定為vm空間下的ViewModel對象上;注:純前臺綁定的關(guān)鍵<i:Interaction.Triggers> <i:EventTrigger EventName="TextChanged"> <i:InvokeCommandAction Command="{Binding NameChanged}" /> </i:EventTrigger></i:Interaction.Triggers>通過觸發(fā)器實現(xiàn)對控件事件的命令綁定,該代碼需要添加System.Windows.Interactivity.dll的引用(BaseClass):
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Windows.Input; 8 9 namespace WpfBing10 {11 public class RelayCommand : ICommand12 {13 #region 字段14 readonly Func<Boolean> _canExecute;15 readonly Action _execute;16 #endregion17 18 #region 構(gòu)造函數(shù)19 public RelayCommand(Action execute)20 : this(execute, null)21 {22 }23 24 public RelayCommand(Action execute, Func<Boolean> canExecute)25 {26 if (execute == null)27 throw new ArgumentNullException("execute");28 _execute = execute;29 _canExecute = canExecute;30 }31 #endregion32 33 #region ICommand的成員34 public event EventHandler CanExecuteChanged35 {36 add37 {38 39 if (_canExecute != null)40 CommandManager.RequerySuggested += value;41 }42 remove43 {44 45 if (_canExecute != null)46 CommandManager.RequerySuggested -= value;47 }48 }49 50 [DebuggerStepThrough]51 public Boolean CanExecute(Object parameter)52 {53 return _canExecute == null ? true : _canExecute();54 }55 56 public void Execute(Object parameter)57 {58 _execute();59 }60 #endregion61 }62 }說明:該段代碼主要實現(xiàn)ICommand命令,實現(xiàn)該命令接口,通過委托調(diào)用調(diào)用ViewModel中相應(yīng)的方法;
ICommand主要有兩個方法,Excute,CanExcute,一個是調(diào)用的實現(xiàn)方法,一個是判斷是否執(zhí)行該調(diào)用方法;
注:功能上可以用來控制按鈕或其他,控件狀態(tài)是否可用
(viewmodel):
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Input;namespace WpfBing{ public class ViewModel:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void Notify(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } private string name = "測試數(shù)據(jù)"; public string Name { get { return name; } set { name = value; Notify("Name"); } } void UpdateArtistNameExecute() { this.Name = "中孝介"; } bool CanUpdateArtistNameExecute() { return true; } public ICommand UpdateData { get { return new RelayCommand(UpdateArtistNameExecute, CanUpdateArtistNameExecute); } } public ICommand NameChanged { get { return new RelayCommand(NameChang); } } private void NameChang() { string na = Name; } }}說明:viewmodel中就是對一些事件流,數(shù)據(jù)流的控制了通過對數(shù)據(jù),控制可以實現(xiàn)刷新前臺數(shù)據(jù),命令控制,可以訪問業(yè)務(wù)層,等下層業(yè)務(wù)等;
新聞熱點
疑難解答