1) xaml View代码:
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="DEMO1.MainPage"
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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:viewModel="clr-namespace:DEMO1.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<viewModel:ItemViewModel x:Key="mainPageViewModel"/>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White" DataContext="{StaticResource mainPageViewModel}">
<!--下面里的SelectedItem必须要用mode为TwoWay,否则ViewModel无法接收到View上发生变化的数据-->
<sdk:DataGrid AutoGenerateColumns="True" SelectedItem="{Binding SelectedItem,Mode=TwoWay}" ItemsSource="{Binding ItemList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectChangedCommand}" CommandParameter="{Binding SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:DataGrid>
<Button Margin="10" Command="{Binding SearchCommand}" Content="SearchData"/>
<Button Margin="10" Command="{Binding UpdateCommand}" Content="UpdateData"/>
<Button Margin="10" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItem}" Content="DeleteSelectedData"/>
</StackPanel>
</UserControl>
2) ViewModel代码:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using DEMO1.Web;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Linq;
using System.Collections.ObjectModel;
namespace DEMO1.ViewModels
{
public class ItemViewModel:NotificationObject
{
ItemsDomainContext _ItemDomainContext = new ItemsDomainContext();
#region 数据属性
//不使用NotificationObject类库,而使用INotifyPropertyChanged 接口实现的:
//public event PropertyChangedEventHandler PropertyChanged;
//private Items _selectedItem = new Items();
//public Items SelectedItem
//{
// get { return _selectedItem; }
// set
// {
// if (value != null)
// {
// this._selectedItem = value;
// PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
// }
// }
//}
//数据属性
private Items selectedItem;
public Items SelectedItem
{
get { return selectedItem; }
set
{
this.selectedItem = value;
this.RaisePropertyChanged("SelectedItem");
}
}
private ObservableCollection<Items> itemList;
public ObservableCollection<Items> ItemList
{
get { return itemList; }
set
{
this.itemList = value;
RaisePropertyChanged("ItemList");
}
}
#endregion 数据属性
#region 命令属性
//命令属性
//public DelegateCommand<string> SearchCommand;
public DelegateCommand SearchCommand { get; set; }
//带参数的命令属性
public DelegateCommand<Items> SelectChangedCommand { get; set; }
public DelegateCommand UpdateCommand { get; set; }
public DelegateCommand<Items> DeleteCommand { get; set; }
#endregion 命令属性
#region 命令属性的委托实现方法
//命令委托的实现(不带参数的Command)
private void SearchCommandExecute()
//命令委托的实现(带参数的Command)
//private void SearchCommandExecute(string para)
{
this.ItemList = new ObservableCollection<Items>();
this.ItemList.Clear();
var query = this._ItemDomainContext.GetItemsQuery();
_ItemDomainContext.Load(query, (lo) =>
{
//查询出来后要显示在界面上的操作
//this.ItemList = lo.Entities; //错误无法执行 由于无法把IEnumerable直接隐士转换 ObservableCollection
//所以我们需要使用foreach来Add对象
foreach (var item in lo.Entities.AsEnumerable())
{
this.ItemList.Add(item);
}
}, null
);
}
//委托命令的实现
private void SelectChangedCommandExecute(Items item)
{
MessageBox.Show(item.SItemCode);
}
//直接在DataGrid上双击进入单元格,更新数据后点击Update即可更新数据。
private void UpdateCommandExecute()
{
_ItemDomainContext.SubmitChanges();
}
//删除所选择的行数据
public void DeleteCommandExecute(Items item)
{
_ItemDomainContext.Items.Remove(item);
_ItemDomainContext.SubmitChanges();
}
#endregion 命令属性的委托实现方法
public ItemViewModel()
{
this.SearchCommandExecute();
this.SearchCommand = new DelegateCommand(new Action(this.SearchCommandExecute));
//为了添加Command的参数需要这样:
//this.SearchCommand = new DelegateCommand<string>(new Action<string>(this.SearchCommandExecute));
this.SelectChangedCommand = new DelegateCommand<Items>(new Action<Items>(this.SelectChangedCommandExecute));
this.UpdateCommand = new DelegateCommand(new Action(this.UpdateCommandExecute));
this.DeleteCommand = new DelegateCommand<Items>(new Action<Items>(this.DeleteCommandExecute));
}
}
}
源代码: http://download.youkuaiyun.com/detail/eric_k1m/6792409