您的位置:首页 > 大数据 > 人工智能

DataGrid行详细信息的绑定--DataGrid.RowDetailsTe(转载)

2013-06-25 18:00 369 查看
在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 { 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下载。

  



  



本文来自程兴亮的博客,原文地址:http://www.cnblogs.com/chengxingliang/archive/2011/07/22/2112895.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐