zoukankan      html  css  js  c++  java
  • Silverlight实用窍门系列:5.绑定webService数据到DataGrid,设置DataGrid模板,模拟数据库数据的绑定【附带实例源码】

           根据第二节说述,我们能够从webService获取到相关的信息数据。那么我们如果要查询数据库的数据,就应该在webservice端使用Select 语句来查询到表,然后使用DataSet的GetXml()方法,获取到相应的XML格式化数据库表数据。在本实例中,我们在webService端模拟一串数据库表的数据。如下:

    <NewDataSet>
                    <Table>
                        <AddrName>四川</AddrName>
                        <CityName>成都</CityName>
                        <TelNum>028</TelNum>
                    </Table>
                    <Table>
                        <AddrName>广东</AddrName>
                        <CityName>广州</CityName>
                        <TelNum>020</TelNum>
                    </Table>
                    <Table>
                        <AddrName>北京</AddrName>
                        <CityName>北京</CityName>
                        <TelNum>010</TelNum>
                    </Table>
    </NewDataSet>
    

           在此XML数据中,我们可以很清晰的看出这个表包含AddrName,CityName,TelNum三个字段,然后这个表中有3行数据。

    我们在Silverlight接收到这串数据的时候,我们需要在Silverlight端声明一个实体类。这个实体类拥有AddrName,CityName,TelNum三个属性,然后再每次实例化这个实体类,将实体类对象添加到一个实体类集合中去。这样子这个实体类对象集合就拥有了3行数据。在这里我们首先看实体类的源代码:

        /// <summary>
        /// 城市信息的实体类
        /// </summary>
        public class CityInformation
        {
            private string _AddrName;
            private string _CityName;
            private string _TelNum;
    
            public string AddrName
            {
                get { return _AddrName; }
                set { _AddrName = value; }
            }
            public string CityName
            {
                get { return _CityName; }
                set { _CityName = value; }
            }
            public string TelNum
            {
                get { return _TelNum; }
                set { _TelNum = value; }
            }
          
        }
    

           在这里,我们接收到XML数据,解析,并且得到实体类对象集合的代码如下:

            List<CityInformation> cityList = new List<CityInformation>();
                 //声明实体类集合,以保存实体类集合
                using (XmlReader xReader = XmlReader.Create(new StringReader(xmlStr)))
                {
                    xReader.Read();
                    while (xReader.Read())
                    {
                        try
                        {
                            xReader.ReadToFollowing("AddrName");
                            string addrName = xReader.ReadElementContentAsString();
                            xReader.ReadToNextSibling("CityName");
                            string cityName = xReader.ReadElementContentAsString();
                            xReader.ReadToNextSibling("TelNum");
                            string telNum = xReader.ReadElementContentAsString();
                            //解析一行数据库XML的数据。
                            CityInformation cityInfo = new CityInformation();
                            cityInfo.AddrName = addrName;
                            cityInfo.CityName = cityName;
                            cityInfo.TelNum = telNum;
                            cityList.Add(cityInfo);
                              //实例化实体类,然后将实体类添加到实体类集合中去。
                        }
                        catch (Exception ex)
                        { }
                       
                    }
                }
                this.ShowCityList.ItemsSource = cityList;
                  //将实体类集合绑定到DataGrid

            另外我们在这里需要设置DataGrid的列模板,因为他自动生成的列不能满足实际项目的需要。所以我们继续看一下XAML源代码:

    <sdk:DataGrid HorizontalAlignment="Left"  AutoGenerateColumns="False"  Margin="28,71,0,0" Name="ShowCityList" VerticalAlignment="Top"  Height="271"  Width="324" >
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTextColumn Header="省会" Binding="{Binding AddrName}" IsReadOnly="True" Width="108"/>
                    <sdk:DataGridTextColumn Header="城市" Binding="{Binding CityName}" IsReadOnly="True" Width="108"/>
                    <sdk:DataGridTextColumn Header="电话区号" Binding="{Binding TelNum}" IsReadOnly="True" Width="108"/>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>
    

           当然在这里我们需要引入域名空间:    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
            在这里我们AutoGenerateColumns="False"设置本DataGrid不自动生成列。

            <sdk:DataGridTextColumn Header="省会" Binding="{Binding AddrName}" IsReadOnly="True" Width="108"/>,本句中设置DataGrid列名为省会,绑定的是CityInformation实体类的AddrName属性。另外设置了本列的默认宽度为108.

            至此,我们就将从webService获取到的数据绑定到了DataGrid。相信网上也会有很多类似的列子。在这里写出来,给需要的人看,也给初步接触Silverlight的TX看。希望能够写得简单明了一些。

           本例采用VS2010+Silverlight 4.0为开发环境。

           如需源码点击 SLReadXMLForDataGrid.rar 下载。

  • 相关阅读:
    Netty章节二十三:Netty自定义实现粘包与粘包
    Netty章节二十二:Netty自定义编解码器
    Netty章节二十一:Netty的ByteBuf
    Netty章节二十:Netty中的理论与实践
    Netty章节十八:Netty Server Start 源码分析
    Netty章节十七:Zero-copy,零拷贝
    Netty章节十六:Java NIO
    Netty章节十五:Nodejs使用gRPC与Java进行远程通信
    UML类图
    Java中虚函数和纯虚函数
  • 原文地址:https://www.cnblogs.com/chengxingliang/p/1955589.html
Copyright © 2011-2022 走看看