简体   繁体   English

加载具有条件字符串格式的数据绑定的XAML失败

[英]Loading XAML having a data-binding with conditional string format fails

I'm loading XAML using XamlReader and I'm having trouble loading it when I data-bind using a conditional string format. 我正在使用XamlReader加载XAML,并且在使用条件字符串格式进行数据绑定时无法加载它。 To be sure that I havn't made any syntax errors I have tried the conditional format in a stand-alone WPF application. 为确保没有语法错误,我在独立的WPF应用程序中尝试了条件格式。 This is the XAML I use for verification: 这是我用于验证的XAML:

<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 : 但是,我想使用XamlReader加载XAML:

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: TextBlock是相同的,但是,对XamlReader.Parse的调用失败,并带有以下异常:

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;' 如果我将晦涩的字符串格式{}{0:;#,##0.00;}替换为';#,##0.00;' the load is succesful. 负载成功。 Unfortunately, the other format I need (the one for positive values) is '#,##0.00;;' 不幸的是,我需要的另一种格式(正值的一种)是'#,##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? 所以我的问题是,当我使用XamlReader.Parse加载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: 当Binding定义为标签而不是标记扩展时,可以加载:

var xaml = @"<TextBlock
  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
  <TextBlock.Text>
    <Binding Path=""Value"" StringFormat=""0:;#,##0.00;""/>
  </TextBlock.Text>
</TextBlock>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM