Silverlight DataGrid(1) 今天找到關於Silverlight DataGrid 的一些技巧,記錄下來,以備後用.

Silverlight DataGrid(1) 今天找到關於Silverlight DataGrid 的一些技巧,記錄下來,以備後用.,第1张

概述Silverlight DataGrid(1) 今天找到關於Silverlight DataGrid 的一些技巧,記錄下來,以備後用.     Using The Silverlight DataGrid If you have ever worked on an application that displayed large amounts of data, one of the corner Silverlight DataGrid(1)

今天找到關於Silverlight DataGrID 的一些技巧,記錄下來,以備後用.

   

Using The Silverlight DataGrID

If you have ever worked on an application that displayed large amounts of data,one of the cornerstones of your application was probably a DataGrID control.  We have provIDed four .NET DataGrIDs over the years,two for ASP.NET and two for windows Forms,but until Now Silverlight and WPF were left out of the party. 

At MIX 2008 we shipped the first prevIEw of the Silverlight DataGrID and a prevIEw of it in WPF was also shown.  Now that it is out there people want to kNow how to use it.  If you are one of those people,then you have come to the right place.  Here's a quick guIDe on how to get up and running with a Silverlight DataGrID.

Step 0: Create A Silverlight Project

Start a new Silverlight Application as outlined in my previous post.  When given the option,choose the default "Add a new Web" option.

Step 1: Add a DataGrID

If everything went smoothly in the prevIoUs step,your project should be loaded and opened to Page.xaml.  Now just find the DataGrID on the ToolBox and drag it into the root layout GrID named "LayoutRoot".

   

This does a few things behind the scenes:

It adds a reference in your Silverlight project to System.windows.Controls.Data It adds an xmlns called "my" to the root UserControl that specifIEs that the DataGrID is in the System.windows.Controls namespace and located in the System.windows.Controls.Data assembly
xmlns:my="clr-namespace:System.windows.Controls; assembly=System.windows.Controls.Data" It adds an instance of the DataGrID as a child of "LayoutRoot"
<my:DataGrID></my:DataGrID>

If you are the type of the person who likes to see things working after each step,feel free to F5 (choose the option to allow deBUGging in the popup) and take in the awesome sight that is an empty DataGrID.

Not much here,so lets fill it with something.

 Step 2: Set the ItemsSource and autoGenerateColumns

The way to make a DataGrID interesting is by giving it some data.  This is done through the DataGrID's ItemsSource property.  This is same property that other controls in WPF and Silverlight,such as ListBox,use to specify where they will get their data.  The one difference here is that you cannot place arbitrary content in it and have it create a collection for you.

Instead you need to provIDe it a collection of anything that implements IEnumerable such as a List or ObservableCollection.

The ItemsSource can be specifIEd inline in XAML such as:

<my:DataGrID x:name="dg" autoGenerateColumns="True">
<
my:DataGrID.ItemsSource>
<!--Something that implements IEnumerable -->
</my:DataGrID.ItemsSource>
</
my:DataGrID>

However,it is more commonly set in code behind,which is what we will do in this example. 

Step 2 A: name the GrID and Set autoGenerateColumns to True

Before we go to the code behind there are two things that we will want to do while we are still in XAML:

<my:DataGrID x:name="dg" autoGenerateColumns="True"></my:DataGrID>

Give the DataGrID the name "dg" Set autoGenerateColumns to true.  If you have worked with prevIoUs DataGrIDs you might have encountered the autoGenerateColumns property.  This is a convenIEnce property that lets you get up and running quickly,since it will tell the DataGrID to look at the items in its ItemsSource and try to create a column to display each fIEld or property.  You should note that columns are only generated when the ItemsSource property is being set or changed,so if you change the value of autoGenerateColumns after the DataGrID already has an ItemsSource specifIEd,no columns will be generated until the next time the ItemsSource property is changed.

Step 2 B: Create and Set the Items Source

Now that the DataGrID is ready to have its ItemsSource set,go to the Page's constructor located in the code behind file for Page.xaml (A handy shortcut to do this from within Page.xaml is F7) and add the following line below InitializeComponent:

C#

public Page()
{
InitializeComponent();
dg.ItemsSource = "H e l l o W o r l d !".Split();
}

VB

Public Sub New()
InitializeComponent()
dg.ItemsSource = "H e l l o W o r l d !".Split()
End Sub

(If you get the build error: "The name 'dg' does not exist in the current context" with the code above be sure to build a second time so that the name has a chance to propigate) 

One of the easIEst ways to generate an IEnumerable collection is String.Split.  When the resulting array is set as the ItemsSource of the DataGrID a column will be automatically generated since autoGenerateColumns is true.  When you run the application,it will look like this:

This is a little better,but so far this Could be done with a ListBox.  Lets add some more complicated data so that we actually need to use a DataGrID.

Add a new class to your Silverlight project (not the Web project) and name it "Data".

Then add a few propertIEs to bind to.

C#

If you are using C#,you can use the great 3.0 Automatic Properties feature.

public class Data
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
public bool Available { get; set; }
}

VB

Public Class Data
Private _firstname As String
Private
_lastname As String
Private
_age As Integer
Private
_available As Boolean

Property
Firstname() As String
Get
Return
_firstname
End Get
Set
(ByVal value As String)
_firstname = value
End Set
End Property

Property
Lastname() As String
Get
Return
_lastname
End Get
Set
(ByVal value As String)
_lastname = value
End Set
End Property

Property
Age() As Integer
Get
Return
_age
End Get
Set
(ByVal value As Integer)
_age = value
End Set
End Property

Property
Available() As Boolean
Get
Return
_available
End Get
Set
(ByVal value As Boolean)
_available = value
End Set
End Property
End Class

Once the Data class is defined,it can Now be used to provIDe data for the DataGrID.  Go back to the code behind file for Page.xaml and replace the prevIoUs ItemsSource assignment with the following.  A useful trick here is to use the C# and VB 3.0 Object Initializer feature to initialize the Data objects as we add them to the List.

C#

public Page()
{
InitializeComponent();
//dg.ItemsSource = "H e l l o W o r l d !".Split();

List<Data> source = new List<Data>();
int itemsCount = 100;

for (int i = 0; i < itemsCount; i++)
{
source.Add(new Data()
{
Firstname = "First",
Lastname = "Last",
Age = i,
Available = (i % 2 == 0)
});
}

dg.ItemsSource = source;
}

VB

Public Sub New()
InitializeComponent()
'dg.ItemsSource = "H e l l o W o r l d !".Split()

Dim Source As List(Of Data) = New List(Of Data)
Dim ItemsCount As Integer = 100

For index As Integer = 1 To ItemsCount
Source.Add(New Data() With _
{ _
.Firstname = "First",_
.Lastname = "Last",_
.Age = index,_      .Available = (index Mod 2 = 0) _
})
Next

dg.ItemsSource = Source
End Sub

When you run this auto generation takes over using reflection to create a column for each property in Data setting the column header to the name of the property and choosing default column types based on the property type.  For instance the Available column is a DataGrIDCheckBoxColumn.

   

Step 3: Simple Customization of the DataGrID

The easIEst way to customize the DataGrID is through a varIEty of propertIEs.  The other two ways are through Styles and Templates which will be covered in future posts.  Some of the most useful propertIEs for customization are:

GrIDlinesVisibility & headersVisibility

These propertIEs are enumerations that control what grIDlines and headers are displayed.

RowBackground & AlternatingRowBackground

These propertIEs are shortcuts to setting the background color for both rows and alternating rows.

ColumnWIDth & RowHeight

These propertIEs set the default column wIDth and default row height.

IsReadonly & CanUserResizeColumns

These propertIEs control if the end user can edit the data in the grID and if the columns can be resized.

For instance if you set the following propertIEs to the following values:

<my:DataGrID x:name="dg" autoGenerateColumns="True"
GrIDlinesVisibility="Horizontal" headersVisibility="Column"
RowBackground="Cornsilk" AlternatingRowBackground="LemonChiffon"
ColumnWIDth="85" RowHeight="30"
IsReadonly="True" CanUserResizeColumns="False"
>
</
my:DataGrID>

You would get this:

Step 4: Enjoy

Now that you have the basics,enjoy using the DataGrID.  Next time I'll go into how to explicitly define and customize columns instead of using auto generation.

总结

以上是内存溢出为你收集整理的Silverlight DataGrid(1) 今天找到關於Silverlight DataGrid 的一些技巧,記錄下來,以備後用.全部内容,希望文章能够帮你解决Silverlight DataGrid(1) 今天找到關於Silverlight DataGrid 的一些技巧,記錄下來,以備後用.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1062668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存