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>

最新文章

  1. const 引起的BUG
  2. STM32 DMA USART ADC
  3. 互联网金融必须知道:O2O、P2P、MRD、BRD、LBS、PV、UV、KPI、MRD、VP、UED....
  4. GitHub详细教程(转载)
  5. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
  6. jquery中获取当前点击对象
  7. how to get sharepoint lookup value
  8. IntelliJ IDEA 控制台 乱码 有效解决办法
  9. [转]ARM/Thumb/Thumb-2
  10. SQL基础学习_03_数据更新
  11. luogu 3413 SAC#1 - 萌数
  12. 【20170521校内模拟赛】热爱生活的小Z
  13. linux 搭建squid代理服务器
  14. js 日期格式 转换 yyyy-MM-dd
  15. PHP mkdir新建目录
  16. openstack nova工作流程
  17. 在springboot中验证表单信息(六)
  18. 用Python读取文件
  19. Android - 内存泄漏 + 垃圾回收(GC)概念
  20. Docker运行python容器

热门文章

  1. VC控件DateTimePicker使用方法
  2. 利用animate.css和es6制作文字向上滚动的效果
  3. Java SE、Java EE、Java ME三者的区别
  4. nginx 访问ssl 的 pem 遇到的权限问题
  5. PAT甲级——A1011 World Cup Betting
  6. Pandas怎样按条件删除行?
  7. 彭亮—Python学习
  8. Luogu P2764 最小路径覆盖问题(二分图匹配)
  9. Leetcode264. Ugly Number II丑数2
  10. Odoo 中的widget