Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » WPF » Value Converter In WPF - Part I

Value Converter In WPF - Part I

In this article series we will see what are Value Converters and how it can be used and usefulness in WPF.

Author Rank :
Page Views : 24533
Downloads : 316
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ConvertersInWPF.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Value Converters

The first thing we should ask is; what is Value Converter(s)?

Well,

to say it in simple words it is a Converter that converts value based on input as Source and provides output as Target.

Hope the following figure make things easier to understand:

image1.gif

As you can see from the above image we have both way arrows in between Source and Target. That means: the value converter not only designed to take input from the target and modify the source, it also works when source data is updated on target. As you are familiar with two way bindings in WPF.

We will straight away create a WPF Application and create simple converters to understand it.

Creating WPF Application

Fire up Visual Studio 2008 and Create a WPF Application, name it as ConvertersInWPF.

image2.gif

Now let's think what simple converter we can use!

Let's say we will create one or more Converters. So we will keep inside a folder:

image3.gif

Now add a class to the Converter Folder, name it as StatusToColor.cs

image4.gif

Here's the idea, we will have a list of Requests and we will display the status of the request in colors.

Let's have the following entity class that represent Request structure.

image4_5.gif

As you see in above code display we have an Enum as Status and we have 5 values inside of it; such as: Submitted, Assigned, InProgress, Resolved, and Closed.

Now based on above information we will modify our StatusToColor.cs

Implement IValueConverter in the above class.

As the namespace is not referred, we have to type it upto end and after resolving you would see the following namespace to be used.

image5.gif

Now we have to implement the methods in this Interface. So resolve the interface again.

image6.gif

Select the Implement interface option: the following methods would be defined as follows:

image7.gif

As we discussed in the beginning that the Source and Target can convert each other so that's why we have two methods defined. Convert would do conversion from Source -> Target and ConvertBack would do conversion from Target -> Source.

For the time being we will proceed with Convert method.

First we will define the Enum here again.

image8.gif

We can use the same name but for understanding I am giving a different name.

Remove the throw new Exception(); and replace with following code.

image9.gif

The above code display for converting and returning is a very simple logic to convert.

Now for user's knowledge about the colr use we would put the colors with some information.

image10.gif

Now we have bind the converter in the XAML, for that first thing we have to do is to use the namespace.

image11.gif

Now we have customized the DataGrid, to have a TextBoxColumn and a TemplateColumn to display the Status as defined Color.

image12.gif

Now for the Rectangle in TemplateColumn we will fill using the Converter:

image13.gif

We are good to go, and run the application. But before that let's have some sample data to display.

image14.gif

You would see the colors are reflected as per the data given.

image15.gif

Hope this article helps.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Diptimaya Patra

Diptimaya is working as a Sr. Software Engineer in Microsoft Technologies (C#). He is a Microsoft MVP in Client App Dev, he has a good hands on in Silverlight 2/3/4, WPF 3/4, Expression Blend 3/4.


Follow him in Twitter: http://www.twitter.com/dpatra

Blog: http://dpatra.blogspot.com , http://diptimayapatra.wordpress.com

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Having trouble with this code by Michele On August 6, 2010
Hi,

I have defined the converter, associated it with the binding as a static resource, and defined the static resource in the user control dictionary.  But I am still getting this error:

Additional information: Cannot add content of type 'System.Windows.Markup.StaticExtension' to an object of type 'System.Windows.ResourceDictionary'. 

Here is the namespace, the static resource and the binding attribute:
    xmlns:converter="clr-namespace:GanttChart.Converters"
     <x:Static x:Key="DateTimeToTimeSpanString" Member="converter:DateTimeToTimeSpanString"/>
     <TextBox Text="{Binding Finish, Converter={StaticResource DateTimeToTimeSpanString}}" FontSize="10" Background="Black" Foreground="WhiteSmoke" BorderBrush="Black"/>


Any ideas? 
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.