Solved

How do I perform formating in Databound field?

Posted on 2009-07-07
10
354 Views
Last Modified: 2013-11-12
I have a text block that I am doing some basic databinding and it works well but I want to have the databound item formated to currency, Can you do this in the XAML?

I have included my code below.
<TextBlock Height="21" Name="txtYTDTotal" Text="{Binding YTD_Total, Mode=TwoWay}" />

Open in new window

0
Comment
Question by:jmarbutt
  • 4
  • 3
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
TSmooth earned 500 total points
ID: 24803544
Yes, look into using a ValueConverter (IValueConverter interface). Here's an article to get you started:
http://www.silverlight-help.com/tips/kentucker/09-03-27/Formatting_bound_data_in_Silverlight_2.aspx
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24806397
well normally valueconverter u have to use when u want to display something based on field value (ie if each different value have different formating).
for simple formatting like this,
you can use Stringformat attribute of Binding.
as the attached code.
http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat.aspx  

<TextBlock Height="21" Name="txtYTDTotal" Text="{Binding YTD_Total, StringFormat={}{0:C}, Mode=TwoWay}" />
 

Open in new window

0
 
LVL 2

Author Comment

by:jmarbutt
ID: 24806926
unmeshdave,

I get an error of

"The property 'StringFormat' was not found in type 'Binding'"

When I try your solution, I am missing something simple.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 8

Expert Comment

by:unmeshdave
ID: 24806941
it comes with .Net 3.5 SP1. so make sure u have installed that.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24806948
0
 
LVL 16

Expert Comment

by:TSmooth
ID: 24807049
unmeshdave, I think he's working with Silverlight which does not support the "StringFormat" binding parameter. That's why I went with IValueConverter.

jmarbutt,
If you are using Silverlight, you will need to do it the way I originally posted and you don't need to install .NET 3.5 SP1. If you are using WPF, then unmeshdave's answer should work and be easier.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24807174
yes if he is using SL then Stringformat will not work as of now.
jmarbutt:
If you are using SL you need to use valueconverter as TSmooth Suggessted.
0
 
LVL 2

Author Comment

by:jmarbutt
ID: 24807212
Yeah that should work then with SL. Sorry for the mix up.
0
 
LVL 16

Expert Comment

by:TSmooth
ID: 25117445
From what I can tell following this thread, jmarbutt used my initial solution.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question