c# - 如何向 xaml 中的绑定(bind)添加附加文本(或字符串)

标签 c# string xaml concatenation binding-context

我有这个:

<Label Text="{Binding Height}" AbsoluteLayout.LayoutBounds=".9,.17,-1,-1" TextColor="White" AbsoluteLayout.LayoutFlags="PositionProportional" HorizontalTextAlignment="Center" HorizontalOptions="Center" FontSize="Medium"/>

显然{Binding Height}占据了整个文本。

绑定(bind)目前是 double 型,并且需要保持如此,我只需要在末尾连接一个“m”来表示米。

我尝试过 {Binding Height} m 和 {Binding Height + m} 但显然 xaml 的工作方式与常规字符串连接的工作方式不同。

最佳答案

尝试使用StringFormat。 像这样:

Text="{Binding Height, StringFormat='{}{0}m'}"

为了清晰起见进行了编辑:

您可以在 {0} 参数后编写任何您想要的内容。

例如,上面将生成 25m、10m 等值。

如果你愿意,你可以写这样的东西:

Text="{Binding Height, StringFormat='{}{0} is a good number.'}"

上面将产生,例如:

10 is a good number.

关于c# - 如何向 xaml 中的绑定(bind)添加附加文本(或字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65238286/

相关文章:

php - 在字符串 PHP 中获取特定字符串

c# - 如何在 Xamarin.Forms 的选取器中显示 ItemDisplayBinding 中的默认枚举值

silverlight - 在 XAML 中创建投影样式

c# - 如何从资源部分中绑定(bind)到数据绑定(bind)的 UserControl?

c# - 设置分块 http 响应的最大块大小

python - 字符串的子集

wpf xaml 面板尺寸

c# - 如何使用 HtmlAgilityPack 向 <head> 添加 <link> 或 &lt;meta&gt; 标签?

c# - 检查当前用户是否活跃?

c - 程序错误之间的字符串