日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

强大的DataGrid组件[1]【转】

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 强大的DataGrid组件[1]【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Cite:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)?

強大的DataGrid組件[1]——Silverlight學習筆記(9)

說明:DataGrid組件是Silverlight數據組件中最為常用并且是功能最為強大的數據組件。因此,對開發者而言,深入了解其特性是十分有必要的。本文先介紹該組件的基本特性,接著通過幾個簡單實例來說明該組件的基本數據操作過程。

?

組件所在命名空間:

System.Windows.Controls

?

組件常用方法:

BeginEdit:使DataGrid進入編輯狀態。

CancelEdit:取消DataGrid的編輯狀態。

CollapseRowGroup:閉合DataGrid的行分組。

CommitEdit:確認DataGrid的編輯完成。

ExpandRowGroup:展開DataGrid的行分組。

GetGroupFromItem:從具體Item中得到分組。

ScrollIntoView:滾動DataGrid視圖。

?

組件常用屬性:

AlternatingRowBackground:獲取或設置一個筆刷用來描繪DataGrid奇數行的背景。

AreRowDetailsFrozen:獲取或設置一個值用來判斷是否凍結每行內容的詳細信息。

AreRowGroupHeadersFrozen:獲取或設置一個值用來判斷是否凍結分組行的頭部。

AutoGenerateColumns:獲取或設置一個值用來判斷是否允許自動生成表列。

CanUserReorderColumns:獲取或設置一個值用來判斷是否允許用戶重新排列表列的位置。

CanUserSortColumns:獲取或設置一個值用來判斷是否允許用戶按列對表中內容進行排序。

CellStyle:獲取或設置單元格的樣式。

ColumnHeaderHeight獲取或設置列頭的高度。

ColumnHeaderStyle:獲取或設置列頭的樣式。

Columns:獲取組件中包含所有列的集合。

ColumnWidth:獲取或設置列寬。

CurrentColumn:獲取或設置包含當前單元格的列。

CurrentItem:獲取包含當前單元格且與行綁定的數據項。

DragIndicatorStyle:獲取或設置當拖曳列頭時的樣式。

DropLocationIndicatorStyle:獲取或設置呈現列頭時的樣式。

FrozenColumnCount:獲取或設置凍結列的個數。

GridLinesVisibility:獲取或設置網格線的顯示形式。

HeadersVisibility:獲取或設置行頭及列頭的顯示形式。

HorizontalGridLinesBrush:獲取或設置水平網格線的筆刷。

HorizontalScrollBarVisibility:獲取或設置水平滾動條的顯示樣式。

IsReadOnly:獲取或設置DataGrid是否為只讀。

MaxColumnWidth:獲取或設置DataGrid的最大列寬。

MinColumnWidth:獲取或設置DataGrid的最小列寬。

RowBackground:獲取或設置用于填充行背景的筆刷。

RowDetailsTemplate:獲取或設置被用于顯示行詳細部分的內容的模板。

RowDetailsVisibilityMode:獲取或設置一個值用以判定行詳細部分是否顯示。

RowGroupHeaderStyles:獲取呈現行分組頭部的樣式。

RowHeaderStyle:獲取或設置呈現行頭的樣式。

RowHeaderWidth:獲取或設置行頭的寬度。

RowHeight:獲取或設置每行的高度。

RowStyle:獲取或設置呈現行時的樣式。

SelectedIndex:獲取或設置當前選中部分的索引值。

SelectedItem:獲取或設置與當前被選中行綁定的數據項。

SelectedItems:獲取與當前被選中的各行綁定的數據項們的列表(List)。

SelectionMode:獲取或設置DataGrid的選取模式。

VerticalGridLinesBrush:獲取或設置垂直網格線的筆刷。

VerticalScrollBarVisibility:獲取或設置垂直滾動條的顯示樣式。

?

組件常用事件:

BeginningEdit:發生于一個單元格或行進入編輯模式之前。

CellEditEnded:發生于一個單元格編輯已被確認或取消。

CellEditEnding:發生于一個單元格正在結束編輯時。

CurrentCellChanged:發生于一個單元格成為當前單元格時。

PreparingCellForEdit:發生于在DataGridTemplateColumn下的單元格進入編輯模式時。

SelectionChanged:發生于當SelectedItemSelectedItems屬性值改變時。

?

實例

DataGrid提供數據源的常用類型主要有兩類:ListObservableCollection。前者一般用于普通數據綁定,而后者則常用于進行數據的雙向綁定來保證數據的同步性。下面就分別給出這兩種DataProvider的例子:

?

List

1)最簡單的綁定

效果圖:

?



MainPage.xaml
文件代碼

<UserControl

????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

????mc:Ignorable="d"?xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"?x:Class="SilverlightClient.MainPage"

????Width="640"?Height="480">

????<Grid?x:Name="LayoutRoot"?Background="White"?Width="640"?Height="480">

????????<data:DataGrid?x:Name="dgEmployee"?Height="188"?HorizontalAlignment="Left"?Margin="18,19,0,0"?VerticalAlignment="Top"?Width="302"/>

????</Grid>

</UserControl>

?

MainPage.xaml.cs文件代碼

using?System;

using?System.Collections.Generic;

using?System.Linq;

using?System.Net;

using?System.Windows;

using?System.Windows.Controls;

using?System.Windows.Documents;

using?System.Windows.Input;

using?System.Windows.Media;

using?System.Windows.Media.Animation;

using?System.Windows.Shapes;

?

namespace?SilverlightClient

{

????public?partial?class?MainPage?:?UserControl

????{

????????public?MainPage()

????????{

????????????InitializeComponent();

????????????this.Loaded +=?new?RoutedEventHandler(MainPage_Loaded);

????????}

?

????????void?MainPage_Loaded(object?sender,?RoutedEventArgs?e)

????????{

????????????string?dataSource =?"H e l l o !";

????????????string[] sp = {?" "?};

????????????dgEmployee.ItemsSource = dataSource.Split(sp,?StringSplitOptions.None).ToList();

????????}

????}

}

2)使用業務對象

效果圖:

?

MainPage.xaml文件代碼

<UserControl

????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

????mc:Ignorable="d"?xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"?x:Class="SilverlightClient.MainPage"

????Width="640"?Height="480">

????<Grid?x:Name="LayoutRoot"?Background="White"?Width="640"?Height="480">

????????<data:DataGrid?x:Name="dgEmployee"?Height="188"?HorizontalAlignment="Left"?Margin="18,19,0,0"?VerticalAlignment="Top"?Width="302"/>

????</Grid>

</UserControl>

?

MainPage.xaml.cs文件代碼

using?System;

using?System.Collections.Generic;

using?System.Linq;

using?System.Net;

using?System.Windows;

using?System.Windows.Controls;

using?System.Windows.Documents;

using?System.Windows.Input;

using?System.Windows.Media;

using?System.Windows.Media.Animation;

using?System.Windows.Shapes;

?

namespace?SilverlightClient

{

????//定義數據類

????public?class?Employees

????{

????????public?int?EmployeeID {?get;?set; }

????????public?string?EmployeeName {?get;?set; }

????????public?int?EmployeeAge {?get;?set; }

?

????????public?Employees()

????????{ }

?

????????public?Employees(int?employeeid,?string?employeename,?int?employeeage)

????????{

????????????EmployeeID = employeeid;

????????????EmployeeName = employeename;

????????????EmployeeAge = employeeage;

????????}

????}

?

????public?partial?class?MainPage?:?UserControl

????{

????????List<Employees> em =?new?List<Employees>();

???????

????????public?MainPage()

????????{

????????????InitializeComponent();

????????????this.Loaded +=?new?RoutedEventHandler(MainPage_Loaded);

????????}

?

????????void?MainPage_Loaded(object?sender,?RoutedEventArgs?e)

????????{

????????????em.Clear();

????????????em.Add(new?Employees(1,?"張三", 23));

????????????em.Add(new?Employees(2,?"李四", 24));

????????????em.Add(new?Employees(3,?"王五", 25));

?

????????????dgEmployee.ItemsSource = em;

????????}

????}

}

?

ObservableCollection

:要實現數據同步的雙向綁定,則業務對象一定要實現INotifyPropertyChanged接口。

效果圖

?


MainPage.xaml
文件代碼

<UserControl

????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

????mc:Ignorable="d"?xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"?x:Class="SilverlightClient.MainPage"

????Width="640"?Height="480">

????<Grid?x:Name="LayoutRoot"?Background="White"?Width="640"?Height="480">

????????<data:DataGrid?x:Name="dgEmployee"?Height="188"?HorizontalAlignment="Left"?Margin="18,19,0,0"?VerticalAlignment="Top"?Width="302"/>

?????????<ListBox?x:Name="lbSynchronization"?HorizontalAlignment="Left"?Margin="18,231,0,71"?Width="302"/>

????</Grid>

</UserControl>

?

MainPage.xaml.cs文件代碼

using?System;

using?System.Collections.Generic;

using?System.Collections.ObjectModel;

using?System.Linq;

using?System.Net;

using?System.Windows;

using?System.Windows.Controls;

using?System.Windows.Documents;

using?System.Windows.Input;

using?System.Windows.Media;

using?System.Windows.Media.Animation;

using?System.Windows.Shapes;

using?System.ComponentModel;

?

namespace?SilverlightClient

{

????public?class?Employees?:?INotifyPropertyChanged

????{

????????private?string?name;

????????public?string??EmployeeName

????????{

??????????get??{?return?name; }

??????????set?

??????????{

????????????if(?value?!= name)

????????????{

??????????????name??=?value;

??????????????onPropertyChanged(this,?"EmployeeName");

????????????}

??????????}

????????}

????????public?event?PropertyChangedEventHandler?PropertyChanged;

?

????????private?void?onPropertyChanged(object?sender,?string?propertyName){

?

????????if(this.PropertyChanged !=?null)

????????{

???????????PropertyChanged(sender,new?PropertyChangedEventArgs(propertyName)) ;

????????}

????}

?

????????public?Employees()

????????{ }

?

????????public?Employees(string?employeename)

????????{

????????????EmployeeName = employeename;

????????}

????}

?

????public?partial?class?MainPage?:?UserControl

????{

????????ObservableCollection<Employees> getEmployeesCollection()

????????{

????????????ObservableCollection<Employees> rVal =?new?ObservableCollection<Employees>();

?

????????????rVal.Add(new?Employees?{ EmployeeName =?"Tim"?});

????????????rVal.Add(new?Employees?{ EmployeeName =?"Mary"?});

????????????rVal.Add(new?Employees?{ EmployeeName =?"John"?});

???????????

????????????return?rVal;

????????}

?

????????public?ObservableCollection<Employees> em;

?

????????public?MainPage()

????????{

????????????InitializeComponent();

????????????this.Loaded +=?new?RoutedEventHandler(MainPage_Loaded);

????????}

?

????????void?MainPage_Loaded(object?sender,?RoutedEventArgs?e)

????????{

????????????em = getEmployeesCollection();

????????????dgEmployee.ItemsSource = em;

????????????lbSynchronization.ItemsSource = em;

????????????lbSynchronization.DisplayMemberPath =?"EmployeeName";

????????}

????}

}

?

作者:Kinglee?
文章出處:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)?
版權聲明:本文的版權歸作者與博客園共有。轉載時須注明本文的詳細鏈接,否則作者將保留追究其法律責任。

轉載于:https://www.cnblogs.com/oneivan/archive/2011/10/07/2200861.html

總結

以上是生活随笔為你收集整理的强大的DataGrid组件[1]【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。