1

I know the below code works with VS2013/.net 4.5.1, but at work we are stuck on .net 4 and cannot upgrade to the latest.net framework (mainly because .net 4.5 is an in place replacement), appreciate if you know of a work around to the below issue. I see that this is a known bug but none of the suggested work around helps me.

I am required to pass a dynamic string format to a text block, since we cannot bind StringFormat, the only alternative I can think of is to parse the literal as below. Below is the code snippets from the app i wrote to prove the issue.

View

<Grid>
    <ListBox x:Name="listBox"/>
</Grid>

Code Behind In the code behind I pass the Stringformat literal as below, in real world i am passing the StringFormat token dynamically

var template ="<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><TextBlock Text=\"{Binding StringFormat={}{0: ###.000; -###.00; -} }\"></TextBlock></DataTemplate>";

var parsedDataTemplate = XamlReader.Parse(template) as DataTemplate;
listBox.ItemTemplate = parsedDataTemplate;

As mentioned the code works like a charm with .net 4.5, is there a workaround or any other hack that I can try for .net 4?

Thanks

Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47

2 Answers2

0

Write a converter to do the formatting. No rule that says you can't convert a string to a string.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Converter was the first thing that came to my mind, but unfortunately we cannot use it, our data model is the same and the UI can represent the same model in atleast 20+ views each with a different format, we do not want to create so many converters, and maintenance will be a night mare. – Rohit Sharma Oct 31 '13 at 01:19
0

There was a typo in my fix.

I was supposed to use "double back slash on both the starting and ending curly brace", i was applying only at the begining. It should have been like so. \\{0: ###.000; -###.00; -\\}

In .net 4.5 and above the back slash is not required any more. StringFormat={}{0: ###.000; -###.00; -}

Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47