gpt4 book ai didi

c# - 加载具有条件字符串格式的数据绑定(bind)的 XAML 失败

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:25 24 4
gpt4 key购买 nike

我正在使用 XamlReader 加载 XAML,当我使用条件字符串格式进行数据绑定(bind)时,我无法加载它。为了确保我没有犯任何语法错误,我在独立的 WPF 应用程序中尝试了条件格式。这是我用于验证的 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>

以及代码隐藏:

public partial class Window {

public Window() {
InitializeComponent();
DataContext = this;
}

public Decimal Value { get { return -1234567.89M; } }

}

正如预期的那样,数值显示时没有负号(如果值为零或正数则不显示)。

但是,我想使用 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);

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: 

如果我将晦涩的字符串格式 {}{0:;#,##0.00;} 替换为 ';#,##0.00;' 负载是成功了。不幸的是,我需要的另一种格式(正值格式)是 '#,##0.00;;' 并且由于某些未知原因,如果值为负,它不会作为条件格式. (它显示带有符号的负数,而不是像它应该做的那样不显示任何东西。“括号”版本没有这个问题。)

所以我的问题是,当使用 XamlReader.Parse 加载 XAML 时,为什么我不能在数据绑定(bind)中使用带括号的条件字符串格式,而当我使用相同的条件字符串格式时使用相同的 XAML 构建 WPF 应用程序?

最佳答案

这不是实际问题的答案,但这可能对您有所帮助。当 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>";

关于c# - 加载具有条件字符串格式的数据绑定(bind)的 XAML 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com