简单的Silverlight+MVVM+WCF Ria Service 对数据的增删改查DEMO

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.csdn.net/detail/eric_k1m/6792409

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值