Solved

How do I perform formating in Databound field?

Posted on 2009-07-07
10
349 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
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24806941
it comes with .Net 3.5 SP1. so make sure u have installed that.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now