大佬教程收集整理的这篇文章主要介绍了Silverlight实用窍门系列:48.DataGrid行详细信息的绑定--DataGrid.RowDetailsTemplate,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Silverlight中的DataGrid控件使用中我们想点击其中一行并且看这一行的详细信息应该如何做呢?而且这个详细信息是多行的数据,而非简单的几个属性。
在这里我们使用DataGrid.RowDetailstemplate来设置或者获取行详细信息。首先我们准备一个DataGrid命名为A,设置其 RowDetailsVisibilitymode="VisibleWhenSELEcted" (行详细信息模板的显示模式是当这行被选中的时候展开这行的详细信息。)然后再为A设置DataGrid.RowDetailstemplate模板,并 且在这个模板中添加一个DataGrid命名为B,这就是前台的XAML代码,在后台中我们设置一个实体集AList绑定到A的DataGrid,然后在 AList实体集中有一个属性是BList,这个就是多行的详细信息。将BList详细信息字段绑定到B的DataGrid控件的Itemssource 即可。
下面我们来看看这个简单的应用技巧的Xaml代码如下:
@H_772_331@
- <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>
@H_772_331@
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.gridemployee.Itemssource = new UserInfo().GetemployeeData();
- }
- }
- /// <sumMary>
- /// 用户信息
- /// </sumMary>
- public class UserInfo
- {
- public String UserName { get; set; }
- public String UserPwd { get; set; }
- /// <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="广州",
- new UserDetailInfo()
- {
- UserAddress="广东省茂名市",
- UserCity="茂名",
- UserState="出生地"
- }
- }
- });
- employees.Add
- (
- new UserInfo
- {
- UserName = "刘敏",
- UserPwd = "motorola",
- UserDetailInfomation = new List<UserDetailInfo>()
- {
- new UserDetailInfo()
- {
- UserAddress="湖南省长沙市",
- UserCity="长沙",
- new UserDetailInfo()
- {
- UserAddress="湖南省长沙市",
- UserCity="长沙",
- UserState="出生地"
- }
- }
- });
- return employees;
- }
- }
- /// <sumMary>
- /// 用户详细信息的实体
- /// </sumMary>
- public class UserDetailInfo
- {
- public String UserAddress { get; set; }
- public String UserCity { get; set; }
- public String UserState { get; set; }
- public String UserCountry { get; set; }
- }
最后我们来看看它的运行@L_874_32@,如果需要源码请点击SLDataGridRowDetail.zip下载。
以上是大佬教程为你收集整理的Silverlight实用窍门系列:48.DataGrid行详细信息的绑定--DataGrid.RowDetailsTemplate全部内容,希望文章能够帮你解决Silverlight实用窍门系列:48.DataGrid行详细信息的绑定--DataGrid.RowDetailsTemplate所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。