wpf - 为什么在此 Xaml 中忽略 StringFormat?

标签 wpf xaml string-formatting

<分区>

我有一个简单的标签,我想显示一些带有内置标签“header”的诊断数据。与其在水平 StackPanel 中使用 2 个标签,不如尝试使用 {Binding StringFormat}。这是 XAML:

   <StackPanel Orientation="Horizontal">
       <Label Content="{Binding Path=SomeData, StringFormat=Value: {0}}" />
   </StackPanel>

在这种情况下,SomeData 是 View 模型上的一个字符串属性,它已正确绑定(bind)到 View 。 SomeData 的值为“ABC”。

我希望标签显示:“值:ABC” 它不是。它简单地显示:“ABC”

我做错了什么?

最佳答案

不确定为什么 StringFormat 不起作用,我猜它不适合 Text 以外的属性。对于 Content 属性,您可以尝试使用 ContentStringFormat 代替:

<Label Content="{Binding Path=SomeData}" ContentStringFormat="Value: {0}" />

有些人建议用 TextBlock 包装 Content 并在 TextBlock 的 Text 属性绑定(bind)中使用 StringFormat [f.e question 1 , question 2 ].

<Label>
    <TextBlock Text="{Binding Path=SomeData, StringFormat='{}Value: {0}'}"/>
</Label>

关于wpf - 为什么在此 Xaml 中忽略 StringFormat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868352/

相关文章:

c# - 子窗口顶部的 WPF 所有者窗口

xaml - 当子控件变换出其父网格或边框时隐藏子控件

c# - c#中的日期时间字符串问题

python - 管理 python 字符串中的关键字参数 : how to format only some arguments and keep the others unformatted

java - 格式化为String()

c# - 数据网格 : Have a stable/fixed row for a "group" of items

c# - 获取组中的选定单选按钮 (WPF)

c# - 如何将一维字符串数组结果绑定(bind)到数据网格列标题

c# - 如何将多个 (n) 多边形绑定(bind)到 Bing map 控件

c# - 如何在 WPF 中更改每列项目的字体大小