view-viewModel-model,模型介紹省略,就是創建類,添加字段封裝屬性。注:控件的綁定只能綁定到屬性上,不能綁定到字段上;
接下來就是代碼
1 <Window x:Class="WpfBing.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:vm="clr-namespace:WpfBing" 5 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 6 Title="MainWindow" Height="350" Width="525"> 7 <Grid> 8 <Grid.DataContext> 9 <vm:ViewModel/> 10 </Grid.DataContext> 11 <TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30"> 12 <i:Interaction.Triggers> 13 <i:EventTrigger EventName="TextChanged"> 14 <i:InvokeCommandAction Command="{Binding NameChanged}" /> 15 </i:EventTrigger> 16 </i:Interaction.Triggers> 17 </TextBox> 18 <Button Content="測試" Command="{Binding UpdateData}" Width="150" Height="40" HorizontalAlignment="Right"> 19 </Button> 20 </Grid> 21 </Window>
說明:
xmlns:vm="clr-namespace:WpfBing"添加對命名空間的引用,主要是讓前台頁面能夠尋找到viewmodel的命名空間;
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 添加wpf的命名控件引用,主要是用來提供使用該命名空間中的觸發器綁定命令
該類的下載鏈接為:System.Windows.Interactivity
<Grid.DataContext> <vm:ViewModel/> </Grid.DataContext> 數據源綁定,將該空間按的數據源綁定為vm空間下的ViewModel對象上;注:純前台綁定的關鍵
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged"> <i:InvokeCommandAction Command="{Binding NameChanged}" /> </i:EventTrigger> </i:Interaction.Triggers>
通過觸發器實現對控件事件的命令綁定,該代碼需要添加System.Windows.Interactivity.dll的引用
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 WpfBing 10 { 11 public class RelayCommand : ICommand 12 { 13 #region 字段 14 readonly Func<Boolean> _canExecute; 15 readonly Action _execute; 16 #endregion 17 18 #region 構造函數 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 #endregion 32 33 #region ICommand的成員 34 public event EventHandler CanExecuteChanged 35 { 36 add 37 { 38 39 if (_canExecute != null) 40 CommandManager.RequerySuggested += value; 41 } 42 remove 43 { 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 #endregion 61 } 62 }
說明:該段代碼主要實現ICommand命令,實現該命令接口,通過委托調用調用ViewModel中相應的方法;
ICommand主要有兩個方法,Excute,CanExcute,一個是調用的實現方法,一個是判斷是否執行該調用方法;
注:功能上可以用來控制按鈕或其他,控件狀態是否可用
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 = "測試數據"; 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中就是對一些事件流,數據流的控制了通過對數據,控制可以實現刷新前台數據,命令控制,可以訪問業務層,等下層業務等;