如何用WPF实现一个最简单的Mvvm示例
1个回答
展开全部
创建一个 ViewModelBase
public abstract class ViewModelBase : INotifyPropertyChanged{
//属性改变事件
public event PropertyChangedEventHandler PropertyChanged;
//当属性改变的时候,调用该方法来发起一个消息,通知View中绑定了propertyName的元素做出调整
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
创建一个DelegateCommand
public class DelegateCommand : ICommand{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
END
创建示例用 ViewModel
让 ViewModel 继承自 ViewModelBase。
public class MainWindowViewModel : ViewModelBase{
private string _input;
public string Input
{
get
{
return _input;
}
set
{
_input = value;
RaisePropertyChanged("Input");
}
}
private string _display;
public string Display
{
get
{
return _display;
}
set
{
_display = value;
RaisePropertyChanged("Display");
}
}
public DelegateCommand SetTextCommand { get; set; }
private void SetText(object obj)
{
Display = Input;
}
public MainWindowViewModel()
{
SetTextCommand = new DelegateCommand(new Action<object>(SetText));
}
}
public abstract class ViewModelBase : INotifyPropertyChanged{
//属性改变事件
public event PropertyChangedEventHandler PropertyChanged;
//当属性改变的时候,调用该方法来发起一个消息,通知View中绑定了propertyName的元素做出调整
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
创建一个DelegateCommand
public class DelegateCommand : ICommand{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
END
创建示例用 ViewModel
让 ViewModel 继承自 ViewModelBase。
public class MainWindowViewModel : ViewModelBase{
private string _input;
public string Input
{
get
{
return _input;
}
set
{
_input = value;
RaisePropertyChanged("Input");
}
}
private string _display;
public string Display
{
get
{
return _display;
}
set
{
_display = value;
RaisePropertyChanged("Display");
}
}
public DelegateCommand SetTextCommand { get; set; }
private void SetText(object obj)
{
Display = Input;
}
public MainWindowViewModel()
{
SetTextCommand = new DelegateCommand(new Action<object>(SetText));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询