Welcome to Office Zealot Sign in | Join | Help

Avalon Adventures Part 5

Part 5 – The Conversion

 

Code for this demo can be found in the downloads section of www.dashpoint.com

 

Once again…Sorry for the delay. I took some time off for New Years, got sick and got behind on work but as promised here’s the next chapter….

 

In this chapter we are going to discuss formatting data using Converters.

 

One of the facilities provided by Avalon is the ability to apply converters to different data elements. This is the mechanism that is used to format data like dates, currency amounts, phone numbers, etc….

 

The Problem

On the order screen there are a number of date fields (order date, ship date, etc). These data fields come from a datetime data type. This means it has a date and a time component. For this sample application I want to show only the date portion of all date fields. So how can I do this… Format the data using SQL syntax or there must be another way….

 

Calling in Converters

 

The first step to adding a converter to your project is to create a class that implements the IValueConverter interface. This interface has two functions: Convert and ConvertBack.  Convert is used to convert your data from one format to another. ConvertBack is use to reverse the process.

 

The following code demonstrates the code for our new converter:

 

Imports System.Windows.Data

 

Public Class DateConverterMaster

    Implements IValueConverter

 

 Public Function Convert(ByVal value As Object, _

   ByVal targetType As System.Type, _

   ByVal parameter As Object, _

   ByVal culture As System.Globalization.CultureInfo) _

   As Object Implements System.Windows.Data.IValueConverter.Convert

 

   Dim RetVal As String = _

      System.Convert.ToDateTime(value).ToShortDateString

    Return RetVal

 

  End Function

 

Public Function ConvertBack(ByVal value As Object, _

   ByVal targetType As System.Type, _

   ByVal parameter As Object, _

   ByVal culture As System.Globalization.CultureInfo) _

   As Object Implements System.Windows.Data.IValueConverter.ConvertBack

  

  Return New Object

End Function

End Class

 

Once you have created your DateConverter class you can apply it to your text boxes. This is done by applying the converter class to binding context of your control(s). To provide efficiency we created a common function on the form that would allow us to apply the date converter to an arbitrarily handed in TextBox control. The following code demonstrates attaching a converter class to a passed in text box control.

 

Sub ConverterHack(ByVal TextBoxToConvert _

    As System.Windows.Controls.TextBox)

  Dim ControlBind As Bind = _

  TextBoxToConvert.GetBinding(_

  System.Windows.Controls.TextBox.TextProperty).ParentBind

 

   'ControlBind.Converter = New DateConverterMaster

   ControlBind.Converter = Me.ScreenDateConverter

End Sub

 

That’s it…. Simply grab the binding context of the textbox’s text field with the GetBindingFunction() and attach your converter from there.

 

Note: IN the code we are attaching the converter to a public form property called ScreenDateConverter. ScreenDateConverter is an instance of our converter class.

 

You could also instantiate a new instance of your converter for each control but that is memory inefficient.

 

For now that concludes my adventures into the Avalon and data binding space. Later this week I plan on putting a new topic up discussing how I felt about this whole process and what my initial impressions of Avalon, XAML and the future are….

 

Published Saturday, January 15, 2005 10:23 AM by rod

Comments

# re: Avalon Adventures Part 5

Wednesday, December 14, 2005 6:08 PM by Anonymous
hgggu uiik l; ;

# re: Avalon Adventures Part 5

Sunday, September 24, 2006 9:54 AM by Anonymous
hi man
Anonymous comments are disabled