zoukankan      html  css  js  c++  java
  • DataGrid行详细信息的绑定--DataGrid.RowDetailsTe(转载)

    在Silverlight中的DataGrid控件使用中我们想点击其中一行并且看这一行的详细信息应该如何做呢?而且这个详细信息是多行的数据,而非简单的几个属性。 在这里我们使用DataGrid.RowDetailsTemplate来设置或者获取行详细信息。首先我们准备一个DataGrid命名为A,设置其RowDetailsVisibilityMode=VisibleWhenSelected (行详细信

      

      在Silverlight中的DataGrid控件使用中我们想点击其中一行并且看这一行的详细信息应该如何做呢?而且这个详细信息是多行的数据,而非简单的几个属性。

      在这里我们使用DataGrid.RowDetailsTemplate来设置或者获取行详细信息。首先我们准备一个DataGrid命名为A,设置其RowDetailsVisibilityMode="VisibleWhenSelected" (行详细信息模板的显示模式是当这行被选中的时候展开这行的详细信息。)然后再为A设置DataGrid.RowDetailsTemplate模板,并且在这个模板中添加一个DataGrid命名为B,这就是前台的XAML代码,在后台中我们设置一个实体集AList绑定到A的DataGrid,然后在AList实体集中有一个属性是BList,这个就是多行的详细信息。将BList详细信息字段绑定到B的DataGrid控件的ItemsSource即可。

      下面我们来看看这个简单的应用技巧的Xaml代码如下:

    <Grid x:Name="LayoutRoot" Background="White">
            <!--这里是第一个DataGrid,其DataGrid.RowDetailsTemplate模板会绑定另外一个DataGrid以显示其详细信息-->
            <sdk:DataGrid x:Name="gridEmployee" CanUserReorderColumns="False" CanUserSortColumns="False" 
                            RowDetailsVisibilityMode=
    "VisibleWhenSelected" 
                            HorizontalAlignment=
    "Center" ScrollViewer.VerticalScrollBarVisibility="Auto" 
                            Height=
    "200"  AutoGenerateColumns="False" Width="422" VerticalAlignment="Center">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Width="150" 
                                            Header=
    "用户名" 
                                            Binding=
    "{Binding UserName}"/>
                <sdk:DataGridTextColumn Width="150" 
                                            Header=
    "用户密码" 
                                            Binding=
    "{Binding UserPwd}"/>
            </sdk:DataGrid.Columns>
            <sdk:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <!--这里是第二个DataGrid显示详细信息-->
                    <sdk:DataGrid  AutoGenerateColumns="False" ItemsSource="{Binding UserDetailInfomation}"
                                    HeadersVisibility=
    "None">
                        <sdk:DataGrid.Columns>
                            <sdk:DataGridTextColumn Width="100" 
                                            Header=
    "地址" 
                                            Binding=
    "{Binding UserAddress}"/>
                            <sdk:DataGridTextColumn Width="100" 
                                            Header=
    "城市" 
                                            Binding=
    "{Binding UserCity}"/>
                            <sdk:DataGridTextColumn Width="100" 
                                            Header=
    "国籍" 
                                            Binding=
    "{Binding UserCountry}"/>
                            <sdk:DataGridTextColumn Width="100" 
                                            Header=
    "类型" 
                                            Binding=
    "{Binding UserState}"/>
                        </sdk:DataGrid.Columns>
                    </sdk:DataGrid>
                </DataTemplate>
            </sdk:DataGrid.RowDetailsTemplate>
        </sdk:DataGrid>
    </Grid>

      然后我们来看看他的数据源的Xaml.cs代码如下:

       public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                this.gridEmployee.ItemsSource = new UserInfo().GetEmployeeData();
            }
        }
        /// <summary>
        /// 用户信息
        /// </summary>
        public class UserInfo
        {
            public string UserName { getset; }
            public string UserPwd { getset; }
            /// <summary>
            /// 用户详细信息
            /// </summary>
            public List<UserDetailInfo> UserDetailInfomation{get;set;}
            public UserInfo()
            { }
            /// <summary>
            /// 获取用户信息的实例
            /// </summary>
            /// <returns></returns>
            public List<UserInfo> GetEmployeeData()
            {
                List<UserInfo> employees = new List<UserInfo>();
                employees.Add
                    (
                    new UserInfo
                    {
                        UserName = "李伟",
                        UserPwd = "1333821",
                        UserDetailInfomation = new List<UserDetailInfo>() 
                        { 
                            new UserDetailInfo()
                            { 
                                    UserAddress="四川省成都市",
                                    UserCity="成都",
                                    UserCountry="中国",
                                    UserState="当前所在地"
                            },
                                new UserDetailInfo()
                            { 
                                    UserAddress="四川省内江市",
                                    UserCity="内江",
                                    UserCountry="中国",
                                    UserState="出生地"
                            }
                        }
                    });
                employees.Add
                    (
                    new UserInfo
                    {
                        UserName = "Json",
                        UserPwd = "json282",
                        UserDetailInfomation = new List<UserDetailInfo>() 
                        { 
                            new UserDetailInfo()
                            { 
                                    UserAddress="广东省广州市",
                                    UserCity="广州",
                                    UserCountry="中国",
                                    UserState="当前所在地"
                            },
                                new UserDetailInfo()
                            { 
                                    UserAddress="广东省茂名市",
                                    UserCity="茂名",
                                    UserCountry="中国",
                                    UserState="出生地"
                            }
                        }
                    });
                employees.Add
                    (
                    new UserInfo
                    {
                        UserName = "刘敏",
                        UserPwd = "motorola",
                        UserDetailInfomation = new List<UserDetailInfo>() 
                        { 
                            new UserDetailInfo()
                            { 
                                    UserAddress="湖南省长沙市",
                                    UserCity="长沙",
                                    UserCountry="中国",
                                    UserState="当前所在地"
                            },
                                new UserDetailInfo()
                            { 
                                    UserAddress="湖南省长沙市",
                                    UserCity="长沙",
                                    UserCountry="中国",
                                    UserState="出生地"
                            }
                        }
                    });
                return employees;
            }
        }
        /// <summary>
        /// 用户详细信息的实体
        /// </summary>
        public class UserDetailInfo
        {
            public string UserAddress { getset; }
            public string UserCity { getset; }
            public string UserState { getset; }
            public string UserCountry { getset; }
    }

      最后我们来看看它的运行效果,如果需要源码请点击SLDataGridRowDetail.zip下载。

      

      

    本文来自程兴亮的博客,原文地址:http://www.cnblogs.com/chengxingliang/archive/2011/07/22/2112895.html

  • 相关阅读:
    技术的极限(8): 集成与分离
    心智与认知(1): 反馈循环(Feedback loop)
    证明与计算(6): 身份认证与授权
    证明与计算(5): 从加密哈希函数到一致性哈希
    技术的极限(6): 密码朋克精神(Cypherpunk Spirit)
    翻译(3): NULL-计算机科学上最糟糕的失误
    工具(5): 极简开发文档编写(How-to)
    证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
    证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)
    翻译(2): How to Write a 21st Century Proof
  • 原文地址:https://www.cnblogs.com/fang-beny/p/3155177.html
Copyright © 2011-2022 走看看