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代码如下:

<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 { 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="广州",
 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 { get; set; }
 public string UserCity { get; set; }
 public string UserState { get; set; }
 public string UserCountry { get; set; }
 }


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值