February 24, 2011

Value Converters

Introduction

If you want to databind two properties that have incompatible types, you need a piece of code in between, that converts the value from source to target type and back. This piece of code is called ValueConverter. A value converter is a class, that implements the simple interface IValueConverter with the two methods object Convert(object value) and object ConvertBack(object value).
How to implement a ValueConverter

WPF already provides a few value converts, but you will soon need to implement your own converts. To do this, add a class to your project and call it [SourceType]To[TargetType]Converter. This is a common naming for value converters. Make it public and implement the IValueConverter interface. That's all you need to do.


public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// Do the conversion from bool to visibility
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
// Do the conversion from visibility to bool
}
}



How to use a ValueConverter in XAML

First thing you need to do is to map the namespace of your converter to a XAML namespace. Then you can create an instance of a value converter in the resources of the view and give it a name. Then you can reference it by using {StaticResource}









Simplify the usage of ValueConvers

If you want to use a normal ValueConverter in XAML, you have to add an instance of it to the resources and reference it by using a key. This is cumbersome, because and the key is typically just the name of the converter.

A simple and cool trick is to derive value converters from MarkupExtension. This way you can create and use it in the binding like this: Text={Binding Time, Converter={x:MyConverter}}, and that is quite cool!


public abstract class BaseConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}



StringFormat Converter

The StringFormatConverter is a useful converter to control the format of an implicit string conversion of an object (e.g. if you bind a DateTime to a TextBlock ).


[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : BaseConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string format = parameter as string;
if (!string.IsNullOrEmpty(format))
{
return string.Format(culture, format, value);
}
else
{
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
}
http://www.wpftutorial.net/ValueConverters.html

Data Binding (WPF)

Windows Presentation Foundation (WPF) data binding provides a simple and consistent way for applications to present and interact with data. Elements can be bound to data from a variety of data sources in the form of common language runtime (CLR) objects and XML. ContentControls such as Button and ItemsControls such as ListBox and ListView have built-in functionality to enable flexible styling of single data items or collections of data items. Sort, filter, and group views can be generated on top of the data.

The data binding functionality in WPF has several advantages over traditional models, including a broad range of properties that inherently support data binding, flexible UI representation of data, and clean separation of business logic from UI.

This topic first discusses concepts fundamental to WPF data binding and then goes into the usage of the Binding class and other features of data binding.

This topic contains the following sections.

MSDN

Routed Event

Functional definition: A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.
Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.
A typical WPF application contains many elements. Whether created in code or declared in XAML, these elements exist in an element tree relationship to each other. The event route can travel in one of two directions depending on the event definition, but generally the route travels from the source element and then "bubbles" upward through the element tree until it reaches the element tree root (typically a page or a window). This bubbling concept might be familiar to you if you have worked with the DHTML object model previously.
http://msdn.microsoft.com/en-us/library/ms742806.aspx#prerequisites

.Net Generics

Introduction

Generics are a new feature in version 2.0 of the C# language and the common language runtime (CLR)

When you use generics, you are creating classes or methods that use a generic type, rather than a specific type. For example, rather than creating a type-specific, you could create a reusable List class using generics.

How is that different from the ArrayList class?

The System.Collection.ArrayList can be used with any objectn, but no type checking is done when objects are passed to methods. You have to manually cast objects back to our type when retrieving; which makes the code harder.

Using the code

I have a Strongly Typed Class named "StudentList" and Generics Class named "MyCustomList" and a sample class named "Student".

"StudentList" class can accepts only Type of Student Objects for its Methods.

But "MyCustomList" Class can Accept any Type u specifying in "T"

Consider the class Student



Student dhas = new Student("Manick", "Dhas", 22);

Student raj = new Student("Sundar", "Raj", 32);

///Using a custom strongly typed StudentList

StudentList mc = new StudentList();
mc.Add(dhas);
mc.Add(raj);

Response.Write("Using a custom strongly typed StudentList
");

foreach (Student s in mc)
{
Response.Write("First Name : " + s.FirstName + "
");

Response.Write("Last Name : " + s.LastName + "
");

Response.Write("Age : " + s.Age + "

");

}
We can use MyCustomList like..

///Creating a list of Student objects using my custom generics

MyCustomList student = new MyCustomList();
student.Add(dhas);
student.Add(raj);

Response.Write("
Using a list of Student objects using my custom generics
");

foreach (Student s in student)
{
Response.Write("First Name : " + s.FirstName + "
");

Response.Write("Last Name : " + s.LastName + "
");

Response.Write("Age : " + s.Age + "

");

}

///Creating a list of Student objects using my custom generics

MyCustomList intlist = new MyCustomList();
intlist.Add(1);
intlist.Add(2);

Response.Write("
Using a list of String values using my custom generics
");

foreach (int i in intlist)
{
Response.Write("Index : " + i.ToString() + "
");

}

///Creating a list of Student objects using my custom generics

MyCustomList strlist = new MyCustomList();
strlist.Add("One");
strlist.Add("Two");

Response.Write("
Using a list of int values using my custom generics
");

foreach (string str in strlist)
{
Response.Write("Index : " + str + "
");

}