【发布时间】:2010-10-21 19:30:40
【问题描述】:
在 WPF 中,有没有办法让 TextBlock 的 Text 属性同时包含硬编码文本和特定绑定?
我想到的是以下内容(当然,以下内容无法编译):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
【问题讨论】:
标签: wpf xaml data-binding textblock
在 WPF 中,有没有办法让 TextBlock 的 Text 属性同时包含硬编码文本和特定绑定?
我想到的是以下内容(当然,以下内容无法编译):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
【问题讨论】:
标签: wpf xaml data-binding textblock
在使用上述方法时:
<TextBlock Text="{Binding Path="Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />
我发现它有些限制,因为我找不到在 StringFormat 中加粗的方法,也无法在 StringFormat 中使用撇号。
相反,我采用了这种方法,这对我来说效果更好:
<TextBlock TextWrapping="Wrap">
<Run>The value</Run>
<Run Text="{Binding Path=MyProperty1, Mode=OneWay}" FontWeight="Bold" />
<Run>was invalid. Please enter it with the format... </Run>
<LineBreak/><LineBreak/>
<Run>Here is another value in the program</Run>
<Run Text="{Binding Path=MyProperty2, Mode=OneWay}" FontWeight="Bold" />
</TextBlock>
【讨论】:
TextBlock中使用2个绑定
使用模板 10 和 MVVM 的 XAML:
要明确一点:
以下是如何将硬编码文本与 Text 属性中的绑定一起使用:
<Page
...
xmlns:vm="using:doubleirish.ViewModels"
xmlns:sys="using:System"
xmlns:controls="using:Template10.Controls"
...
<Page.DataContext>
<vm:StocksViewModel x:Name="ViewModel" />
</Page.DataContext>
...
<controls:PageHeader ... Text="{x:Bind sys:String.Format('Ticker : {0}', ViewModel.Ticker)}">
...
</Page>
【讨论】:
这里的绑定值(clouds.all)加上“%”。您可以在“\{0\}”之后添加所需的任何值。
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>
【讨论】:
有,如果您使用的是 .Net 3.5 SP1
<TextBlock Text="{Binding Path=Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />
【讨论】:
<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>
【讨论】: