C# Windows Phone 8.1 XAML字符串格式
我试图显示一些文本和绑定数据,例如,我有以下代码:C# Windows Phone 8.1 XAML字符串格式,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,我试图显示一些文本和绑定数据,例如,我有以下代码: <TextBlock Text="{Binding Shorthand}" Style="{ThemeResource ListViewItemTextBlockStyle}" /> 我想在“速记”之前添加一些文本,根据我所读的内容,可以使用StringFormat作为绑定的属性,大致如下: <TextBlock Text="{Binding Path=Shorthand, StringFormat={0} text
<TextBlock Text="{Binding Shorthand}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
我想在“速记”之前添加一些文本,根据我所读的内容,可以使用StringFormat作为绑定的属性,大致如下:
<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>
并在绑定中使用它:
<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编写):
它工作得很好 “似乎不起作用”不是诊断的主要部分。您是否缺少“in StringFormat=”{0}文本我要添加“?我得到的错误是:“在类型“Binding”中找不到属性“StringFormat””。您可以使用edit将其添加到问题中。