WPF では、Text
a のプロパティにTextBlock
ハードコードされたテキストと特定のバインディングの両方を含める方法はありますか?
私が念頭に置いているのは、次の行に沿ったものです(もちろん、以下はコンパイルされません):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
WPF では、Text
a のプロパティにTextBlock
ハードコードされたテキストと特定のバインディングの両方を含める方法はありますか?
私が念頭に置いているのは、次の行に沿ったものです(もちろん、以下はコンパイルされません):
<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>
.Net 3.5 SP1を使用している場合はあります
<TextBlock Text="{Binding Path=Artist.Fans.Count,
StringFormat='Number of Fans: {0}'}" />
上記のアプローチを使用する場合:
<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 Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>