1基础目录结构如下:
2.SharedDatas.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfPlatform.ViewModels;
namespace WpfPlatform.Common
{
public class SharedDatas
{
public static ViewModel VM = new ViewModel();
}
}
3.DelegateCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfPlatform.Models
{
public class DelegateCommand : ICommand
{
Func<object, bool> canExecute;
Action<object> executeAction;
public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (canExecute == null)
{
return true;
}
return canExecute.Invoke(parameter);
}
//public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
executeAction(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
#endregion
}
}
4.Person.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfPlatform.Models
{
public class Person : INotifyPropertyChanged, IDataErrorInfo
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Name = "";
public string Name
{
get { return _Name; }
set
{
_Name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
private int _Age = 0;
public int Age
{
get { return _Age; }
set
{
_Age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
public Person()
{
}
public Person(string name, int age)
{
Name = name;
Age = age;
}
public string Error
{
get
{
return "";
}
}
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Name":
if (string.IsNullOrEmpty(Name))
{
return "Name can not be empty";
}
break;
case "Age":
if (string.IsNullOrEmpty(Name))
{
return "Age can not be empty";
}
break;
default:
break;
}
return "";
}
}
}
}
5.ViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfPlatform.Models;
namespace WpfPlatform.ViewModels
{
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Person> _Persons = new ObservableCollection<Person>();
public ObservableCollection<Person> Persons
{
get { return _Persons; }
set
{
_Persons = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Persons"));
}
}
}
private Person _TempPerson = new Person();
public Person TempPerson
{
get { return _TempPerson; }
set
{
_TempPerson = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("TempPerson"));
}
}
}
public DelegateCommand AddPersonCommand { get; private set; }
public DelegateCommand DelPersonCommand { get; private set; }
private bool CanAddPerson(object arg)
{
if (TempPerson.Name!="")
{
return true;
}
return false;
}
private void AddPerson(object obj)
{
Person person = new Person(TempPerson.Name, TempPerson.Age);
Persons.Add(person);
}
public ViewModel()
{
AddPersonCommand = new DelegateCommand(AddPerson, CanAddPerson);
DelPersonCommand = new DelegateCommand(DelPerson, CanDelPerson);
}
private bool CanDelPerson(object arg)
{
if (TempPerson.Name!="")
{
var query = Persons.Where(s => s.Name == TempPerson.Name).FirstOrDefault();
return query != null;
}
return false;
}
private void DelPerson(object obj)
{
var query = Persons.Where(s => s.Name == TempPerson.Name).FirstOrDefault();
Persons.Remove(query);
}
}
}
6 xaml
<Window x:Class="WpfPlatform.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:WpfPlatform"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid Margin="5" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False"
ItemsSource="{Binding Persons}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Name" Binding="{Binding Path=Name}"></DataGridTextColumn>
<DataGridTextColumn Width="*" Header="Age" Binding="{Binding Path=Age}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Margin="5" Grid.Column="1">
<Grid DataContext="{Binding TempPerson}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="3" Text="Name" />
<TextBox Margin="3" Grid.Column="1" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Margin="3" Grid.Row="1" Text="Age"/>
<TextBox Margin="3" Grid.Row="1" Grid.Column="1" Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
<Button x:Name="btnAdd" Margin="3" Content="Add" Command="{Binding AddPersonCommand}"></Button>
<Button x:Name="btnDel" Margin="3" Content="Del" Command="{Binding DelPersonCommand}" ></Button>
</StackPanel>
</Grid>
</Window>
7.后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfPlatform.Common;
using WpfPlatform.Models;
using WpfPlatform.ViewModels;
namespace WpfPlatform
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private ViewModel vm = SharedDatas.VM;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
vm.Persons.Add(new Person() { Age = 10, Name = "zs" });
vm.Persons.Add(new Person() { Age = 20, Name = "ls" });
vm.Persons.Add(new Person() { Age = 30, Name = "ww" });
vm.Persons.Add(new Person() { Age = 40, Name = "zl" });
this.DataContext = vm;
}
}
}