当前位置: 代码迷 >> C# >> WPF采取MVVM模式(绑定:纯前台、命令:触发器绑定命令)
  详细解决方案

WPF采取MVVM模式(绑定:纯前台、命令:触发器绑定命令)

热度:706   发布时间:2016-05-05 03:09:45.0
WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

MVVM绑定

view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性。注:控件的绑定只能绑定到属性上,不能绑定到字段上;

接下来就是代码

(view):

 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的引用

(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 构造函数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 }

说明:该段代码主要实现ICommand命令,实现该命令接口,通过委托调用调用ViewModel中相应的方法;

ICommand主要有两个方法,Excute,CanExcute,一个是调用的实现方法,一个是判断是否执行该调用方法;

注:功能上可以用来控制按钮或其他,控件状态是否可用

(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 = "测试数据";        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中就是对一些事件流,数据流的控制了通过对数据,控制可以实现刷新前台数据,命令控制,可以访问业务层,等下层业务等;