
在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="广州", 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; } }
最后我们来看看它的运行效果,如果需要源码请点击SLDataGridRowDetail.zip下载。
总结以上是内存溢出为你收集整理的Silverlight实用窍门系列:48.DataGrid行详细信息的绑定--DataGrid.RowDetailsTemplate全部内容,希望文章能够帮你解决Silverlight实用窍门系列:48.DataGrid行详细信息的绑定--DataGrid.RowDetailsTemplate所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)