gpt4 book ai didi

WPF 绑定(bind) - StringFormat - 不格式化

转载 作者:行者123 更新时间:2023-12-03 13:04:53 27 4
gpt4 key购买 nike

我有一个工具提示,其值设置为:

Value="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}"

工具栏显示为:

72



但我希望它是:

Reserved (Equity Share: 72%)



我的绑定(bind)有什么问题?

最佳答案

工具提示是一个内容控件,这意味着它实际上没有显示模型。这在@deccyclone 的较早答案中得到了证明,该答案将内容设置为 TextBlock。由于 TextBox 旨在显示文本,因此 StringFormat 绑定(bind)属性按宣传的方式工作。按钮是另一个例子。 (两者都派生自 ContentControl)

如果将工具提示的内容设置为字符串,则会显示该字符串,因为如果数据类型为字符串,则工具提示具有内置转换器。如果您想利用内置的字符串转换器,您需要使用 ContentStringFormat 属性设置格式。

<ToolTip
Content="{Binding Path=Value}"
ContentStringFormat="{}{0:F2} M"
/>

顺便说一句,何时使用 StringFormat 或 ContentStringFormat 的提示是控件提供的用于设置显示文本的属性。
文本属性 -> 使用 StringFormat
内容属性 -> 使用 ContentStringFormat

关于WPF 绑定(bind) - StringFormat - 不格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498649/

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