【发布时间】:2017-07-15 01:36:57
【问题描述】:
我不明白为什么我的 MarkupExtension StringFormat 不能与标签一起使用,但可以完美地与 TextBlocks 一起使用。
这是我的代码的简化版本:
[MarkupExtensionReturnType(typeof (BindingExpression))]
public class Format : MarkupExtension
{
#region Public Properties
public BindingBase Binding { get; set; }
public IValueConverter Converter { get; set; }
public string StringFormat { get; set; }
#endregion
#region Public Methods and Operators
public override object ProvideValue(IServiceProvider serviceProvider)
{
this.Binding.StringFormat = this.StringFormat;
return this.Binding.ProvideValue(serviceProvider);
}
#endregion
}
还有 XAML:
<TextBlock Text="{wpfApplication31:Format Binding={Binding Name}, StringFormat=X_{0}}" /> <!-- StringFormat WORKING -->
<Label Content="{wpfApplication31:Format Binding={Binding Name}, StringFormat=X_{0}}" /> <!-- StringFormat NOT WORKING -->
属性“名称”是一个简单的字符串。
我认为这与 Label 是一个比 TextBlock 更复杂的对象有关,但我仍然不明白为什么不应用 StringFormat。
如果有人可以提供帮助。 谢谢
【问题讨论】:
-
我知道我可以在 Binding 中实现 StringFormat,但正如我所说,它是一个简化版本,我需要 MarkupExtension 中的 StringFormat。
标签: xaml string-formatting markup-extensions