你的位置:首页 > 软件开发 > ASP.net > WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

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

发布时间:2015-07-24 15:00:06
MVVM绑定view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性。注:控件的绑定只能绑定到属性上,不能绑定到字段上;接下来就是代码(view): 1 <Window x:Class="WpfBing.MainWindow" ...

MVVM绑定

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

接下来就是代码

(view):

 1 <Window x:Class="WpfBing.MainWindow" 2     "http://schemas.microsoft.com/get='_blank'>winfx/2006/xaml/presentation" 3     "http://schemas.microsoft.com/winfx/2006/xaml" 4     "clr-namespace:WpfBing" 5     "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>

(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 }

原标题:WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

关键词:wpf

wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。