引用:Telerik(官 网:http://www.telerik.com/)是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件。我们提供高度稳定性和丰富性能的组件产品,并可应用在非常严格的环境中。现在拥有 Microsoft, HP, Alcoa, BP, Harper Collins, Siemens, T-Mobile, HJ Heinz和一些最主要的教育机构和很多政府机关等客户。
我在使用的版本是RadControls_for_Silverlight4_2011_1_0316,开发工具采用VS2010 +SQLServer2008R2进行测试学习
Grid是我们做系统必不可少的控件了,所以我准备从这个开始,为自己留下学习痕迹。
先是了解控件属性便于下一步学习,常用属性
RadGridView
常用属性 |
说明 |
DEMO名称 |
AutoGenerateColumns="False" |
是否自动产生列 |
|
IsReadOnly="True" IsReadOnly="{Binding IsChecked, Mode=TwoWay, ElementName=IsReadOnlyCheckBox}" |
是否只读 |
Click Event command |
ShowGroupPanel="False" |
是否显示分组面板(常用,一般情况下是不会用到这个的) |
|
DataLoadMode="Asynchronous" |
数据加载模式 |
|
RowIndicatorVisibility="Collapsed" |
行指示 |
|
SelectionMode="Extended" |
Command | |
CanUserDeleteRows="{Binding IsChecked, Mode=TwoWay, ElementName=CanUserDeleteRowsCheckBox}" |
是否可删除行 |
Command |
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" |
横向竖向滚动条设定 |
|
IsFilteringAllowed="False" |
列过滤查询是否可用 |
Enable/Disable |
ShowColumnFooters="True" |
是否显示列脚 |
Totals |
ShowGroupFooters="True" |
是否显示分组列脚 |
Totals |
GridLinesVisibility= Both Horizontal Vertical |
网络线设置 |
gridlinevisiblity |
GridViewDataColumn
属性 |
说明 |
DEMO名称 |
IsGroupable="False" |
列是否加入分组 |
|
IsFilterable="False" |
列是否可过滤查询 |
|
IsSortable="False" |
列是否可排序 |
|
DataFormatString="{}{0:c2}" DataFormatString="{}{0:d}" |
列输入格式 |
GridViewDataColumn.FilteringControl :可自定义表头查询控件
1、产生自动编号-Row Number
· 自定义列与绑定列并存
XAML <Grid> <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Customers}" AutoGeneratingColumn="RadGridView1_AutoGeneratingColumn"> <telerik:RadGridView.Columns> <custom:MyColumn Header="#" Width="50" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid>
· 自动绑定后的列取消功能(如果取消的列较多,还是需要考虑用别的方法替代)
private void RadGridView1_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e) { if (e.Column.UniqueName == "Order" || e.Column.UniqueName == "Product") { e.Cancel = true; } }· 自定义列的处理
public class MyColumn : Telerik.Windows.Controls.GridViewColumn { public override FrameworkElement CreateCellElement (Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem) { TextBlock textBlock = cell.Content as TextBlock; if (textBlock == null) { textBlock = new TextBlock(); } textBlock.Text = (this.DataControl.Items.IndexOf(dataItem) + 1).ToString(); return textBlock; } }
2、行、列、单元格只读设置
private void ChangeEnableStateColumn(bool isEnabled) { GridViewHeaderRow headerRow = RadGridView1.ChildrenOfType <GridViewHeaderRow>().FirstOrDefault(); if (headerRow != null) { GridViewCellBase cell = (from c in headerRow.Cells where c.Column.UniqueName == "EmployeeID" select c).FirstOrDefault(); if (cell != null) { cell.IsEnabled = isEnabled; } } foreach (object item in RadGridView1.Items) { GridViewRow row = RadGridView1.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow; if (row != null) { GridViewCellBase cell = (from c in row.Cells where c.Column.UniqueName == "EmployeeID" select c).FirstOrDefault(); if (cell != null) { cell.IsEnabled = isEnabled; } } } } private void ChangeEnableStateCell(bool isEnabled) { if (RadGridView1.Items.Count > 0) { GridViewRow row = RadGridView1.ItemContainerGenerator.ContainerFromItem (RadGridView1.Items[0]) as GridViewRow; if (row != null) { GridViewCellBase cell = (from c in row.Cells where c.Column.UniqueName == "EmployeeID" select c).FirstOrDefault(); if (cell != null) { cell.IsEnabled = isEnabled; } } } } private void ChangeEnableStateRow(bool isEnabled) { if (RadGridView1.Items.Count > 0) { GridViewRow row = RadGridView1.ItemContainerGenerator.ContainerFromItem (RadGridView1.Items[0]) as GridViewRow; if (row != null) { row.IsEnabled = isEnabled; } } }
XAML <Grid> <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Customers}" AutoGeneratingColumn="RadGridView1_AutoGeneratingColumn"> <telerik:RadGridView.Columns> <custom:MyColumn Header="#" Width="50" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid>
3、设置网格线颜色
private void VerticalGridLinesColorPicker_SelectedColorChanged(object sender, EventArgs e) { RadGridView1.VerticalGridLinesBrush = new SolidColorBrush (VerticalGridLinesColorPicker.SelectedColor); } private void HorizontalGridLinesColorPicker_SelectedColorChanged(object sender, EventArgs e) { RadGridView1.HorizontalGridLinesBrush = new SolidColorBrush (HorizontalGridLinesColorPicker.SelectedColor); }
4、保存用户对网格的设置
DEMO中saveandload settings中的Radgridviewsetting.cs非常用用:)
本文来自forrestsun 的博客,原文地址:http://www.cnblogs.com/forrestsun/archive/2011/05/13/2045859.html