我有这个:
<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/