zoukankan      html  css  js  c++  java
  • MVVMDemo

    QueryCommand.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    namespace MVVMDemo.Commands
    {
    public class QueryCommand :ICommand
    {
    #region Fields
    private Action _execute;
    private Func<bool> _canExecute;
    #endregion

    public QueryCommand(Action execute)
    : this(execute, null)
    {
    }
    public QueryCommand(Action execute, Func<bool> canExecute)
    {
    if (execute == null)
    throw new ArgumentNullException("execute");
    _execute = execute;
    _canExecute = canExecute;
    }

    #region ICommand Member

    public event EventHandler CanExecuteChanged
    {
    add
    {
    if (_canExecute != null)
    {
    CommandManager.RequerySuggested += value;

    }
    }
    remove
    {
    if (_canExecute != null)
    {
    CommandManager.RequerySuggested -= value;

    }
    }
    }

    public bool CanExecute(object parameter)
    {
    return _canExecute == null ? true : _canExecute();
    }

    public void Execute(object parameter)
    {
    _execute();
    }
    #endregion
    }
    }

    Persons.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections.ObjectModel;
    using MVVMDemo.Model;

    namespace MVVMDemo.DataHelper
    {
    public class PersonDataHelper
    {
    public static ObservableCollection<Person> GetPersons()
    {
    ObservableCollection<Person> samplePersons = new ObservableCollection<Person>();
    samplePersons.Add(new Person() {Name = "张三", Age = 33});
    samplePersons.Add(new Person() { Name ="王五", Age= 22 });
    samplePersons.Add(new Person() { Name = "李四", Age = 35 });
    samplePersons.Add(new Person() { Name = "LearningHard", Age = 27 });
    return samplePersons;
    }
    }
    }

    Person.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace MVVMDemo.Model
    {
    public class Person
    {
    public string Name { get; set; }
    public int Age { get; set; }
    }
    }

    PersonListViewModel.cs

    using MVVMDemo.Commands;
    using MVVMDemo.DataHelper;
    using MVVMDemo.Model;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    namespace MVVMDemo.ViewModel
    {
    public class PersonListViewModel : INotifyPropertyChanged
    {
    #region Fields
    private string _searchText;
    private ObservableCollection<Person> _resultList;
    #endregion

    #region Properties

    public ObservableCollection<Person> PersonList { get; private set; }

    // 查询关键字
    public string SearchText
    {
    get { return _searchText; }
    set
    {
    _searchText = value;
    RaisePropertyChanged("SearchText");
    }
    }

    // 查询结果
    public ObservableCollection<Person> ResultList
    {
    get { return _resultList; }
    set
    {
    _resultList = value;
    RaisePropertyChanged("ResultList");
    }
    }

    public ICommand QueryCommand
    {
    get { return new QueryCommand(Searching, CanSearching); }
    }

    #endregion

    #region Construction
    public PersonListViewModel()
    {
    PersonList = PersonDataHelper.GetPersons();
    _resultList = PersonList;
    }

    #endregion

    #region Command Handler
    public void Searching()
    {
    ObservableCollection<Person> personList = null;
    if (string.IsNullOrWhiteSpace(SearchText))
    {
    ResultList = PersonList;
    }
    else
    {
    personList = new ObservableCollection<Person>();
    foreach (Person p in PersonList)
    {
    if (p.Name.Contains(SearchText))
    {
    personList.Add(p);
    }
    }
    if (personList != null)
    {
    ResultList = personList;
    }
    }
    }

    public bool CanSearching()
    {
    return true;
    }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Methods
    private void RaisePropertyChanged(string propertyName)
    {
    // take a copy to prevent thread issues
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
    handler(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    #endregion
    }
    }

    PersonsView.xaml

    <Window x:Class="MVVMDemo.View.PersonsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MVVMDemo.ViewModel"
    Title="PersonsView" Height="350" Width="400">
    <!--设置DataContex是ViewModel类,当然你也可以使用后台代码设置-->
    <Window.DataContext>
    <local:PersonListViewModel />
    </Window.DataContext>
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="50"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Name="searchtxt" Text="{Binding Path=SearchText, Mode=TwoWay}" HorizontalAlignment="Left" Height="30" Width="280" Margin="10,0,0,0"></TextBox>
    <Button Grid.Row="0" Name="searchBtn" Content="Search" Command="{Binding Path=QueryCommand}" Width="80" Height="30" HorizontalAlignment="Right" Margin="0,0,10,0"></Button>
    <DataGrid Grid.Row="1" Name="datGrid"
    HorizontalAlignment="Center"
    VerticalAlignment="Top" ItemsSource="{Binding Path=ResultList}" Width="300"></DataGrid>

    </Grid>
    </Window>

  • 相关阅读:
    解决“已禁用对分布式事务管理器(MSDTC)的网络访问”错误
    C# Freely convert between IList<T> and IEnumerable<T>
    Json MaxJsonLength Error
    [转]Introducing the IE8 Developer Tools JScript Profiler
    页面调试心得
    ASP.NET中Label控件特殊之处
    模板模式
    Android中通过typeface设置字体
    Android 新浪微博授权
    【转】android:网络图片转为bitmap 保存至SD卡中
  • 原文地址:https://www.cnblogs.com/Jeely/p/11076913.html
Copyright © 2011-2022 走看看