Pergunta

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

Foi útil?

Solução 2

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; -}

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top