轉(zhuǎn)帖|其它|編輯:郝浩|2011-07-29 14:55:58.000|閱讀 691 次
概述:上一篇文章寫得查詢比較簡單,這次做一個基于MVVM下的增刪改。只要按照步驟來,沒有不會的。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
上一篇文章寫得查詢比較簡單,這次做一個基于MVVM下的增刪改。只要按照步驟來,沒有不會的。
第一步:創(chuàng)建一個silverlight項目;
第二步:添加項目對 GalaSoft.MvvmLight.Extras.SL4,GalaSoft.MvvmLight.SL4, System.Windows.Controls.Data, Microsoft.Practices.Unity.Silverlight的引用
第三步:創(chuàng)建相應(yīng)的文件夾ViewModel和Model 和View 。
第四步:在Model文件夾下新建一個類Student.cs類,代碼如下:
public class Student
{
public string Name { get; set; }//名字
public int Age { get; set; } //年齡
public int SNo { get; set; }//學(xué)號
}
第五步:還是在Model文件夾下新建一個類Students.cs類 ,這個類的作用是用于構(gòu)造數(shù)據(jù)。
private ObservableCollection<Student> _getstudents;
public ObservableCollection<Student> GetStudent()
{
_getstudents = new ObservableCollection<Student>(){
new Student{ Name="張三", Age=21, SNo=11},
new Student{ Name="李四", Age=22, SNo=12},
new Student{ Name="王五", Age=23, SNo=13},
new Student{ Name="高尚", Age=24, SNo=14},
};
return _getstudents;
}
第六步:在ViewModel下創(chuàng)建StudentViewModel.cs類,這個類主要是連接model和view的
第七步:從這開始就是怎么把ViewModel里的數(shù)據(jù)輸送到前臺的。
首先在ViewModel里創(chuàng)建ViewModelLocator.cs,我們叫它ViewModel加載器
里面的代碼如下:
using GalaSoft.MvvmLight;
using Microsoft.Practices.Unity; //需要引入Microsoft.Practices.Unity.Silverlight 命名空間
public class ViewModelLocator
{
public static IUnityContainer Container
{
get;
private set;
}
//創(chuàng)建一個容器,把StudentViewModel 扔到這個容器里,前臺只需要在這個容器里取值就行了。
static ViewModelLocator()
{
Container = new UnityContainer();
Container.RegisterType<StudentViewModel>(new ContainerControlledLifetimeManager());
}
/// <summary>
/// 金喜正規(guī)買球操作
/// </summary>
public StudentViewModel MyStu
{
get
{
return Container.Resolve <StudentViewModel>();
}
}
}
第二步:在App.xaml 里添加代碼,引用ViewModelLocator.cs里的東東。
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App"
xmlns:d="//schemas.microsoft.com/expression/blend/2008" xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
這個就是viewmodel那個文件夾
xmlns:vm="clr-namespace:SilverlightApplication1.ViewModel"
mc:Ignorable="d">
應(yīng)用整個viewmodel里的東西。別名是Locator
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>
</Application>
第八步:前臺處理
這樣一個簡單的查詢就做完了。注意這里,MainPage.xaml沒有寫一句代碼
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="//schemas.microsoft.com/expression/blend/2008"
xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
引入的一些東西,有的具體是干嘛的,我也不太懂。
需要引入System.Windows.Controls.Data命名空間 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:i="//schemas.microsoft.com/expression/2010/interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
注意觀察這個 MyStu對應(yīng)的是ViewModelLocator.cs里的類名 Source={StaticResource Locator} 還記得那個別名嗎?呵呵,就是這個
DataContext="{Binding MyStu, Mode=TwoWay, Source={StaticResource Locator}}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" >
全局調(diào)用MyStu對應(yīng)類下面的RefreshCommand。就是StudentViewModel下的RefreshCommand ,進(jìn)行查詢
<i:Interaction.Triggers>
<i:EventTrigger>
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding RefreshCommand, Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid x:Name="LayoutRoot" Background="White" Height="312" Width="442" >
ItemsSource="{Binding Students, Mode=TwoWay}" 綁定數(shù)據(jù)源Students
<data:DataGrid Height="177" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="22,109,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="374" ItemsSource="{Binding Students, Mode=TwoWay}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="姓名" Width="70" Binding="{Binding Name, Mode=TwoWay}" d:IsLocked="True"/>
<data:DataGridTextColumn Header="年齡?" Width="70" Binding="{Binding Age, Mode=TwoWay}" d:IsLocked="True"/>
<data:DataGridTemplateColumn Header="刪除" Width="80" d:IsLocked="True">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
調(diào)用StudentViewModel下的DelCommand方法,實現(xiàn)刪除。
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="刪除" Command="{Binding MyStu.DelCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding Mode=OneWay}" ></Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</UserControl>
第二講:添加我們直接從第六步開始寫,首先StudentViewModel
下寫一個添加的方法。 AddCommand = new RelayCommand(() =>
{
Student stu = new Student();
stu.Name = stuName;
stu.Age = stuAge;
stu.SNo = randmom.Next(1, 100);
Students.Add(stu);
});
這樣,前臺添加按鈕綁定上AddCommand 就可以了。
前臺
<Button Content="添加¨®" Height="23" HorizontalAlignment="Right" Margin="0,53,59,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding AddCommand, Mode=TwoWay}" />
StuName綁定的是對應(yīng)的屬性。不是model里的屬性,是在StudentViewModel下創(chuàng)建的屬性。
<TextBox Height="23" HorizontalAlignment="Left" Margin="70,53,0,0" Name="txtName" VerticalAlignment="Top" Text="{Binding StuName, Mode=TwoWay}" Width="87" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="209,53,0,0" Name="txtAge" VerticalAlignment="Top" Text="{Binding StuAge, Mode=TwoWay}" Width="83" />
第三講:刪除,會了前2個,估計刪除也比較簡單了。
應(yīng)為刪除按鈕我放在的是DataGrid里面,
Command 下的屬性是 MyStu.DelCommand 而添加直接是AddCommand,為什么呢?
應(yīng)為刪除是在DataGrid里面,里面已經(jīng)綁定了數(shù)據(jù)源Students 而Students 沒有DelCommand方法,所以需要到StudentViewModel去找。所以要用全局的MyStu去找DelCommand方法
這里可能理解的不太懂,還請高手指教。
<DataTemplate>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="刪除" Command="{Binding MyStu.DelCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding Mode=OneWay}" ></Button>
</DataTemplate>
做完上面練習(xí),基本上也就會做了。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園