牛骨文教育服务平台(让学习变的简单)

1)视图层后台代码,很简洁,主要是处理视图模型的创建,当然,如果你利用一些技术,这里是可以不需要代码的。不过我的观点是任何事情都不要搞成洁癖,如果页面的所有控制都放在视图模型层,反而会使得视图模型层最后就变成了视图的后台代码,那么这个MVVM模式就失去了意义。

using MEntities;  
using RIAServices.Web;  
using System.Threading;  
using System.Dynamic;  
using System.Windows.Data;  
using MAppStructure.ViewModel;  
namespace MAppStructure  
{  
     
   public partial class MainPage : UserControl  
   {

  
       public DynamicDataViewModel ViewModel  
       {  
           get {  
               return this.DataContext as DynamicDataViewModel;  
           }  
           set{  
               this.DataContext = value;  
           }  
       }  
       public MainPage()  
       {  
           InitializeComponent();

           //也可以通过其它方式进行构建。在简单应用中,这个文件就基本不用自己写代码了。  
       this.ViewModel = new DynamicDataViewModel();  
       }

2)页面绑定.

<sdk:DataGrid x:Name="CustomGrid" ItemsSource="{Binding Path=DataTable.Rows}" Grid.Row="1" AutoGenerateColumns="False" >  
           <sdk:DataGrid.Columns>  
               <sdk:DataGridTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeID].Value,Mode=OneWay}" CanUserResize="True" CanUserSort="True" Width="Auto" />  
               <sdk:DataGridTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeName].Value,Mode=OneWay}" CanUserResize="True" CanUserSort="True" Width="Auto" />  
               <sdk:DataGridTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeDesc].Value,Mode=OneWay}" CanUserResize="True" CanUserSort="True" Width="Auto" />  
               <sdk:DataGridTextColumn CanUserReorder="True" Binding="{Binding Path=[EmployeeAge].Value,Mode=OneWay}" CanUserResize="True" CanUserSort="True" Width="Auto" />  
           </sdk:DataGrid.Columns>  
       </sdk:DataGrid>

注意上面的绑定方式和路径语法。

<Button Content="查询" Command="{Binding Path=Commands[Button1Command]}" CommandParameter="{Binding ElementName=CustomGrid,Path=SelectedItem}"  Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="344,160,0,0" Name="Search"  VerticalAlignment="Center" Width="75"/>

这中命令处理模式,稍微改进一下,就可以做成自动动态绑定,比一个个定义命令的方式要好很多,有利于维护的集中处理。

到此为止,这个系列就完成了,上面代码都是经过测试的,实际运行的。只是如果你的数据量比较大的话,需要修改服务端Web.Config的配置,增大可序列化对象的最大数。相关问题处理可上网搜,很多的。另外消息机制也可以采用,这个可以利用微软的轻量级框架去做,但切勿烂用。

PS:希望大家多提意见,如有更好的方法希望能不吝赐教。