I'm loading XAML using XamlReader
and I'm having trouble loading it when I data-bind using a conditional string format. To be sure that I havn't made any syntax errors I have tried the conditional format in a stand-alone WPF application. This is the XAML I use for verification:
<Window
x:Class="WpfApplication.Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Text="{Binding Value, StringFormat={}{0:;#,##0.00;}}"/>
</Window>
And the code-behind:
public partial class Window {
public Window() {
InitializeComponent();
DataContext = this;
}
public Decimal Value { get { return -1234567.89M; } }
}
As expected the numerical value is displayed without the negative sign (and not displayed if the value is zero or positive).
However, I want to load the XAML using XamlReader
:
var xaml = @"<TextBlock
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
Text=""{Binding Value, StringFormat={}{0:;#,##0.00;}}""/>";
var textBlock = (TextBlock) XamlReader.Parse(xaml);
The TextBlock
is identical, however, the call to XamlReader.Parse
fails with an exception:
System.Windows.Markup.XamlParseException occurred Message='Unexpected token after end of markup extension.' Line number '3' and line position '40'. Source=PresentationFramework LineNumber=3 LinePosition=40 StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri) at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode) at System.Windows.Markup.XamlReader.Load(XmlReader reader) at System.Windows.Markup.XamlReader.Parse(String xamlText) at WpfApplication.Window..ctor() in WpfApplication\Window.xaml.cs:line 17 InnerException: System.Xaml.XamlParseException Message='Unexpected token after end of markup extension.' Line number '3' and line position '40'. Source=System.Xaml LineNumber=3 LinePosition=40 StackTrace: at MS.Internal.Xaml.Parser.MePullParser.d__0.MoveNext() at MS.Internal.Xaml.Parser.XamlPullParser.d__6f.MoveNext() at MS.Internal.Xaml.Parser.XamlPullParser.d__14.MoveNext() at MS.Internal.Xaml.Parser.XamlPullParser.d__7.MoveNext() at MS.Internal.Xaml.Parser.XamlPullParser.d__0.MoveNext() at MS.Internal.Xaml.NodeStreamSorter.ReadAheadToEndOfAttributes() at MS.Internal.Xaml.NodeStreamSorter.ReadAheadAndSortCtorProperties() at MS.Internal.Xaml.NodeStreamSorter..ctor(XamlParserContext context, XamlPullParser parser, XamlXmlReaderSettings settings, Dictionary`2 xmlnsDictionary) at System.Xaml.XamlXmlReader.Initialize(XmlReader givenXmlReader, XamlSchemaContext schemaContext, XamlXmlReaderSettings settings) at System.Xaml.XamlXmlReader..ctor(XmlReader xmlReader, XamlSchemaContext schemaContext, XamlXmlReaderSettings settings) at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode) InnerException:
If I replace the obscure string format {}{0:;#,##0.00;}
with ';#,##0.00;'
the load is succesful. Unfortunately, the other format I need (the one for positive values) is '#,##0.00;;'
and for some unknown reason it doesn't behave as a conditional format if the value is negative. (It displays the negative number with the sign instead of not displaying anything as it is supposed to do. The "bracket" version doesn't have this problem.)
So my question is why can't I use a conditional string format with brackets in data-binding when the XAML is loaded using XamlReader.Parse
when the same conditional string format is fine in when I build a WPF application using the same XAML?
It's not an answer to the actual question, but this may be helpful for you. Loading is ok when Binding defined as tag, not markup extension:
var xaml = @"<TextBlock
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<TextBlock.Text>
<Binding Path=""Value"" StringFormat=""0:;#,##0.00;""/>
</TextBlock.Text>
</TextBlock>";