StringFormat in XAML

狂风中的少年 提交于 2019-12-05 06:43:53

It seems that, similar to Binding in WinRT, Binding in Windows Phone Universal Apps doesn't have StringFormat property. One possible way to work around this limitation is using Converter as explained in this blog post,

To summarize the post, you can create an IValueConverter implmentation that accept string format as parameter :

public sealed class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return null;

        if (parameter == null)
            return value;

        return string.Format((string)parameter, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        string language)
    {
        throw new NotImplementedException();
    }
}

Create a resource of above converter in your XAML, then you can use it like this for example :

<TextBlock x:Name="countTextBlock" 
           Text="{Binding Count, 
                          Converter={StaticResource StringFormatConverter},
                          ConverterParameter='{}{0:n}'}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!