C# MVVM,ICommand的簡單實現
阿新 • • 發佈:2018-11-21
Model部分
實現INotifyPropertyChanged介面,使其能夠發出某一屬性值發生改變的通知
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MVVMTest.Model { public class PersonModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public PersonModel() { Name = "name"; Age = "20"; Sex = "男"; } public string name; public string Name { get { return name;} set { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name)); } } public string age; public string Age { get { return age; } set { age = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age")); } } public string sex; public string Sex { get { return sex; } set { sex = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Sex")); } } } }
ViewModel部分:
首先實現Icommand介面,包含一個Action和Func委託。
public class DeleteCommand<T> : ICommand { public event EventHandler CanExecuteChanged; public Action<T> _execute = null; public Func<T, bool> _canExecute = null ; public DeleteCommand(Action<T> execute ):this(execute,null) { } public DeleteCommand(Action<T> execute, Func<T, bool> canExecute) { if (canExecute != null) { this._canExecute = new Func<T, bool>(canExecute); } if (execute != null) { this._execute = new Action<T>(execute); } } public void RaiseCanExceuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } } public bool CanExecute(object parameter) { if (this._canExecute == null) { return true; } return _canExecute((T)(object)(parameter)); } public void Execute(object parameter) { this._execute((T)(object)(parameter)); } } }
PersonViewModel類作為資料來源,其中包含Model例項和一個DeleteCommand命令;
public class PersonViewModel { // public delegate void Fun(PersonModel person); public PersonModel Model{ set; get; } public DeleteCommand<PersonModel> deleteCommand { set; get; } public PersonViewModel() { Model = new PersonModel(); deleteCommand = new DeleteCommand<PersonModel>( ClearData ,IsPersonNotEmpty); } public void ClearData(PersonModel person) { // PersonModel personModel = person as PersonModel; Model.Age = ""; Model.Name = ""; Model.Sex = ""; } public bool IsPersonNotEmpty(PersonModel person) { // PersonModel personModel = person as PersonModel; // if (string.IsNullOrEmpty(personModel.Name) && string.IsNullOrEmpty(personModel.Age) && string.IsNullOrEmpty(personModel.Sex)) // return false; return true; } }
View部分
<Window x:Class="MVVMTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMTest"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="525">
<Grid Margin="0,0,0,0">
<StackPanel Margin="0,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBox Width="100" Text="{Binding Model.Name}" Height="30" Margin="10" />
<Button x:Name="btn" Content="Clear" Command="{Binding deleteCommand}" Width="100" Height="30" Margin="150,10,10,10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Age" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBox Width="100" Text="{Binding Model.Age}" Height="30" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Sex" Width="100" Margin="10" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBox Width="100" Text="{Binding Model.Sex}" Height="30" Margin="10"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
程式執行:
點選Clear按鈕