我正在尝试显示一些文本以及绑定(bind)数据,例如,我有代码:
<TextBlock Text="{Binding Shorthand}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
我想在“速记”之前添加一些文本,据我所知,这可以通过使用 StringFormat 作为绑定(bind)的属性来实现,大致如下:
<TextBlock Text="{Binding Path=Shorthand, StringFormat={0} text I want to add}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
但是这似乎不起作用,在 8.1 中这不再是做事的方式了吗?
最佳答案
StringFormat
在 WinRT 上不受支持。但是,您可以通过创建自定义转换器轻松替换它:
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>
并在您的绑定(bind)中使用它:
<TextBlock Text="{Binding Path=SomeText, Converter={StaticResource ResourceKey=StringFormat}, ConverterParameter='Hello {0}'}" />
关于c# - Windows Phone 8.1 XAML 字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24127262/