zoukankan      html  css  js  c++  java
  • MVVMlight框架应用:Data Binding、Command

    常用Wpf开发中我们在ViewModel中实现INotifyPropertyChanged接口,通过触发PropertyChanged事件达到通知UI更改的目的;在MVVMLight框架里,这里我们定义的ViewModel都继承自ViewModelBaseViewModelBase封装在MvvmLight框架中,它已经实现了INotifyPropertyChanged接口,因此我们在定义ViewModel属性时,只需要调用RaisePropertyChanged(PropertyName)就可以进行属性更改通知了。例如下代码:

           // DatePicker 选中日期
            private DateTime _SelectedDate;
            public DateTime SelectedDate
            {
                get
                {
                    return _SelectedDate;
                }
    
                set
                {
                    if (_SelectedDate == value)
                        return;
    
                    _SelectedDate = value;
    
                    RaisePropertyChanged("SelectedDate");
                }
            }

     事件是WPF/SL应用程序中UI与后台代码进行交互的最主要方式,与传统方式不同,mvvm中主要通过绑定到命令来进行事件的处理,因此要了解mvvm中处理事件的方式,就必须先熟悉命令的工作原理。

    一、RelayCommand命令

    WPF/SL命令是通过实现 ICommand 接口创建的。 ICommand 公开两个方法(ExecuteCanExecute)和一个事件(CanExecuteChanged)。 Execute 执行与命令关联的操作。CanExecute 确定是否可以在当前命令目标上执行命令。在MvvmLight中实现ICommand接口的类是RelayCommand,RelayCommand通过构造函数初始化ExecuteCanExecute方法,因此,构造函数传入的是委托类型的参数,ExecuteCanExecute则执行的是委托的方法,RelayCommand相关代码如下:

            public RelayCommand(Action execute, Func<bool> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }
    
                _execute = execute;
                _canExecute = canExecute;
            }
    
            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute();
            }
    
            public void Execute(object parameter)
            {
                _execute();
            }

    二、 Comand属性绑定

    简单的例子:一个TextBox和一个Button,TextBox非空内容时候Button才可用,可用的Button点击后把TextBox内容show出来。

    <Window x:Class="MVVMLightDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:MVVMLightDemo.ViewModel"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <local:MainWindowViewModel></local:MainWindowViewModel>
        </Window.DataContext>
        <Grid>
            <StackPanel>
                <TextBox Text="{Binding UserName, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
                <Button Content="show" Command="{Binding ShowCommand}" CommandParameter="{Binding UserName}"></Button>
            </StackPanel>
        </Grid>
    </Window>

    ViewMode:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    //
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using System.Windows.Input;
    using System.Windows;
    namespace MVVMLightDemo.ViewModel
    {
        public class MainWindowViewModel:ViewModelBase
        {
            private string _UserName;
    
            public string UserName
            {
                get { return _UserName; }
                set { _UserName = value; RaisePropertyChanged("UserName"); }
            }
            public ICommand ShowCommand
            {
                get
                {
                    return new RelayCommand<string>(
                        (user) => {
                            MessageBox.Show(user);
                        }, (user) =>
                        {
                            return !string.IsNullOrEmpty(user);
                        });
    
                }
            }
        }
    }
  • 相关阅读:
    this直接加在函数或者是 “原型”对象的区别
    网易云课堂_C语言程序设计进阶_第一周:数据类型:整数类型、浮点类型、枚举类型_1计算分数精确值
    #include <stdbool.h>
    网易云课堂_C语言程序设计进阶_第一周:数据类型:整数类型、浮点类型、枚举类型
    #include <locale.h> #include <locale>
    C语言复习
    Apache HTTP Server
    深拷贝_浅拷贝
    04737_C++程序设计_第4章_类和对象
    经典算法_链表
  • 原文地址:https://www.cnblogs.com/xiepeixing/p/3255152.html
Copyright © 2011-2022 走看看