c# - Windows Phone 8.1 XAML 字符串格式

标签 c# xaml windows-phone-8.1

我正在尝试显示一些文本以及绑定(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/

相关文章:

c# - 无法使用C# HttpClient将数据发布到nodejs服务器

wpf - WTF WPF TabControl?

windows-phone-8 - Windows Phone 8.1 后台任务

wpf - 选择但失去焦点时更改 WPF treeViewItem 背景颜色

wpf - 如何更改 Xaml AppBarButton (Windows UWP) 的标签位置

c# - 下载图像时异步/等待死锁

debugging - GroupStyle HidesIfEmpty 中是否有错误?

c# - 使用 MARS 的缺点(多个事件结果集)

c# - 如何从 XAML 中的 DataTemplate 引用匹配对象?

c# - Visual C# 找不到我的 Ionic Zip .dll 文件