[英]Windows Phone 8.1 XAML StringFormat
我试图显示一些文本以及绑定数据,例如,我有代码:
<TextBlock Text="{Binding Shorthand}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
我想在'速记'之前添加一些文本,从我读过的内容可以通过使用StringFormat作为Binding的属性来实现,这有点类似于:
<TextBlock Text="{Binding Path=Shorthand, StringFormat={0} text I want to add}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
然而,这似乎不起作用,这是不是在8.1中做事的方式?
WinRT不支持StringFormat
。 但是,您可以通过创建自定义转换器轻松替换它:
public class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return string.Format(parameter as string, value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return null;
}
}
然后在页面资源中声明它:
<Page.Resources>
<local:StringFormatConverter x:Name="StringFormat"/>
</Page.Resources>
并在绑定中使用它:
<TextBlock Text="{Binding Path=SomeText, Converter={StaticResource ResourceKey=StringFormat}, ConverterParameter='Hello {0}'}" />
就像@KooKiz指出的那样,目前不支持StringFormat
,但你可以完成同样的效果,只需将你的行分解为内联运行而不需要像转换器一样;
<TextBlock>
<Run Text="Hey I wanted to put this text in front of "/>
<Run Text="{Binding Path=Shorthand}"/>
<Run Text=" and I also wanted some text after it. Neato.."/>
</TextBlock>
希望这会有所帮助,欢呼。
我使用这种方法(由Microsoft编写): https : //msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter
它很棒!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.