惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
L
LangChain Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园_首页
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
美团技术团队
V
V2EX

博客园 - yo

Silverlight 缺陷 - yo - 博客园 SilverLight Controls WF 4.0 中 Persistence 异常 - yo SilverLight框架初探-RiaService SilverLight框架初探-ViewModel SilverLight框架初探 与客户“调情” AG_E_PARSER_BAD_PROPERTY_VALUE 定义属于自己的Routing 数据契约的序列化 WCF客户端配置问题 关闭EXCEL进程 母版页中控件ID获取 - yo - 博客园 Reportviewer - Error: ASP.NET session has expired - yo SQL CLR C# DLL动态调用 Sharepoint List faults - yo 出错页面webpar的t删除 quickpart
SilverLight框架初探-View - yo - 博客园
yo · 2010-03-17 · via 博客园 - yo

前面分别实现了ViewModel与Riaservice,接下来让我们看看如果实现与View的绑定,如下:

ProductionView.xaml的代码如下:

<UserControl x:Class="Mvvm.Client.Views.ProductionView"
    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:vm="clr-namespace:Mvvm.Client.ViewModels.ViewModels;assembly=Mvvm.Client.ViewModels"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
    <!--添加ProductionViewModel引用-->
    <UserControl.Resources>
        <vm:ProductionViewModel x:Name="productionViewModel"/>
    </UserControl.Resources>
    <!--通过DataContext与Grid绑定-->
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource productionViewModel}}" MinHeight="100">
        <!--添加GdataGrid与DataPager控件-->
        <data:DataGrid AutoGenerateColumns="True" Name="dataGrid1" MinHeight="20" MinWidth="100" Grid.Row="4" />
        <data:DataPager Grid.Row="5" Name="dataPager1" />
    </Grid>
</UserControl>

在这里,控件与数据源的绑定放在了其*.cs文件中,这样更方便于我们进行相关操作,代码如下:

 public partial class ProductionView : UserControl
    {
        public ProductionView()
        {
            InitializeComponent();
            BindingGrid();
        }

        void BindingGrid()
        {
            this.productionViewModel = new ViewModels.ViewModels.ProductionViewModel();
            base.DataContext = this.productionViewModel;
            this.productionViewModel.QueryCommand.Execute(null);

            // Wrap the itemList in a PagedCollectionView for paging functionality
            PagedCollectionView itemListView = new PagedCollectionView(this.productionViewModel.ListProduction);
            if (itemListView != null)
            {
                dataGrid1.ItemsSource = itemListView;
                dataPager1.Source = itemListView;
                dataPager1.PageSize = 20;
                dataPager1.DataContext = this.dataPager1;
            }
        }
    }

这样,我们就能够将View与ViewModel关联起来。

以MVVM这么模式来设计开发Silverlight,真正意义上达到了我们要实现页面与逻辑分离的目的。上面例子中,我们的View也可以用WPF来实现。