StringFormatConverter not working?

Mar 18, 2012 at 3:57 PM

I'm trying out the StringFormatConverter (in a 7.1 project) for the first time but it doesn't seem to be working.

In the source it says:

Format a value as hex: {Binding Foo, Converter={StaticResource myStringFormatConverter}, Parameter={0:x}}

However the only format that looks valid is this:

{Binding Foo, Converter={StaticResource myStringFormatConverter}, ConverterParameter={0:x}}

This doesn't work either though as the compiler complains it can't understand the value passed to the ConverterParameter (actual error is "The tag '0' does not exist in the XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'."

Has anyone got the converter to work and if so, how?

Thanks.

Coordinator
Mar 19, 2012 at 4:43 PM

Yes, take a look at the Flickr sample, in PhotoView you'll see: 

  <ScrollViewer x:Name="svDescription"  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="0">
                            <StackPanel>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Description}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="2,0" />                                                                
                                <TextBlock Text="{Binding Converter={StaticResource stringFormatConverter}, Path=Posted, ConverterParameter=Posted: \{0:ddd\, MMM dd\, yyyy mm:hh:ss\}}" Style="{StaticResource PhoneTextSubtleStyle}" FontStyle="Italic" FontWeight="Bold" HorizontalAlignment="Left"/>
                                <TextBlock Text="{Binding Converter={StaticResource stringFormatConverter}, Path=Owner.UserName, ConverterParameter=By: \{0\}}" Style="{StaticResource PhoneTextSubtleStyle}" FontStyle="Italic" FontWeight="Bold" HorizontalAlignment="Left"/>
                            </StackPanel>                            
                        </ScrollViewer>

You need to escape the brackets, I think that's the problem you're seeing.

Mar 19, 2012 at 4:55 PM

Ahh, thanks. I didn't look at that example. Yeah, the escaping is the issue. Thanks for that.