本文介绍了的Windows Phone 8.1 XAML的StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示与绑定数据一起一些文本,比如,我的代码:

I am trying to display some text along with binded data, for example, I have the code:

<TextBlock Text="{Binding Shorthand}"  Style="{ThemeResource ListViewItemTextBlockStyle}" />



我想之前速记添加一些文字,从我读这将有可能通过使用的StringFormat为一体的结合,东西线沿线的一个属性:

I want to add some text before 'Shorthand', from what I have read this would be possible by using StringFormat as a property of the Binding, something along the lines of:

<TextBlock Text="{Binding Path=Shorthand, StringFormat={0} text I want to add}"  Style="{ThemeResource ListViewItemTextBlockStyle}" />



不过,这似乎并没有工作,这是不再做的事情在8.1的方式吗?

However this doesn't seem to work, is this no longer the way to do things in 8.1?

推荐答案

的StringFormat 不支持WinRT的。但是,你可以很容易地创建一个自定义转换器替换:

StringFormat isn't supported on WinRT. However, you can easily replace it by creating a custom converter:

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;
    }
}



然后在你的页面资源,声明它:

Then declare it in your page resources:

<Page.Resources>
    <local:StringFormatConverter x:Name="StringFormat"/>
</Page.Resources>



而在你的绑定使用它:

And use it in your bindings:

<TextBlock Text="{Binding Path=SomeText, Converter={StaticResource ResourceKey=StringFormat}, ConverterParameter='Hello {0}'}" />

这篇关于的Windows Phone 8.1 XAML的StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 05:05