【轉】RadControls for Silverlight(學習1-GridView)

引用:Telerik(官 網:http://www.telerik.com/)是保加利亞的一個軟件公司,專一於微軟.Net平臺的表示層與內容管理控件。咱們提供高度穩定性和豐富性能的組件產品,並可應用在很是嚴格的環境中。如今擁有 Microsoft, HP, Alcoa, BP, Harper Collins, Siemens, T-Mobile, HJ Heinz和一些最主要的教育機構和不少政府機關等客戶。html

我在使用的版本是RadControls_for_Silverlight4_2011_1_0316,開發工具採用VS2010 +SQLServer2008R2進行測試學習web

Grid是咱們作系統必不可少的控件了,因此我準備從這個開始,爲本身留下學習痕跡。網絡

先是瞭解控件屬性便於下一步學習,經常使用屬性ide

RadGridView工具

經常使用屬性性能

說明學習

DEMO名稱開發工具

AutoGenerateColumns="False"測試

是否自動產生列this

 

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>

三、設置網格線顏色

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);
}      

四、保存用戶對網格的設置

DEMO中saveandload settings中的Radgridviewsetting.cs很是用用:)

 

引用地址:http://www.cnblogs.com/forrestsun/archive/2011/05/13/2045859.html

相關文章
相關標籤/搜索