Solved

How do I perform formating in Databound field?

Posted on 2009-07-07
10
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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