Avalon Adventures Part 2
NorthWind Avalon Part 2 – Basic Data Binding
Code for this demo can be found in the downloads section of www.dashpoint.com
I like Ward Cunningham’s description of a Wiki. The smallest thing that could actually work.
This is how I dive off into any project. I want to do the most basic thing to try something out. So here’s the task…. Get a customer record and display the name in a text field. Pretty simple huh… Lets take a look.
First Step is to create a new Avalon Application. You can now create/compile/run your Avalon projects in Visual Studio 2005 (aka Whidbey)
Before you can try any of this stuff you need the Avalon/WinFX CTP installed.
So go create a new project in Whidbey….
Now you need to define a few items for display. By default creating an Avalon Window populates your form with a XAML tag. The tag give you the ability to create a set of Rows and Columns in which you can place controls. For this example we will create a grid with 1 row and 2 columns.
Now that you have defined your grid you can place controls in it. We will place a text control in the left column for our caption and a textbox control in our right column.
Now our code looks like this.
Grid.Row="0"
Grid.Column="0"
TextContent="Company" />
Grid.Row="0"
Grid.Column="1"
ID="txtCompanyName"
>
Text="*Bind(Path=CompanyName)"
The grid class is pretty flexible in placing controls. Notice the Grid.Row and Grid.Column settings. This allows you to place your controls wherever you want.
Now lets go get some data and bind the control.
Open the codebehind code for your window.
You will need add the following statement at the top of your code
Imports System.Data
Next add a property to your class that loads up data for everyones favorite customer ALFKI.
Dim CustomerData as Dataset = DataAccess.GetCustomer(‘ALFKI’)
Next we want to make sure our Window calls the equivalent of the Load event of the window. To do this you need to add the following code to your XAML window definition.
Loaded="OnLoaded"
Your window definition now looks as follows:
xmlns="http://schemas.microsoft.com/2003/xaml"
xmlns:x="Definition"
Text="Customer Entry"
Loaded="OnLoaded"
>
Now go back into the code behind, uncomment the OnLoaded function that was created by VS.NET And add the following code
Me.DataContext = Me.CustomerData.Tables(“customers”)
Now the last step…. You need to bind the text property of your text box to the CustomerName column. To do this add the following attribute to your TextBox.
Text="*Bind(Path=CompanyName)"
This will bind the value found in the CompanyName field into the text attribute of your TextBox. Your TextBox should look like this:
Grid.Row="0"
Grid.Column="1"
ID="txtCompanyName"
Text="*Bind(Path=CompanyName)"
>
Now go run your code. You should see the company name for ALFKI displayed in the text box.
Next Chapter.... Creating a list of customers.......