Evo*_*lor 6 data-binding xaml string-formatting windows-phone-8.1
我试图格式化我string每3个地方有逗号,如果不是整数,则为小数.我检查了大约20个例子,这是我最接近的例子:
<TextBlock x:Name="countTextBlock" Text="{Binding Count, StringFormat={0:n}}" />
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个The property 'StringFormat' was not found in type 'Binding'.错误.
任何想法在这里有什么不对?Windows Phone 8.1似乎与WPF不同,因为所有WPF资源都说这就是它的工作方式.
(这string是不断更新的,所以我需要代码XAML.我还需要它保持绑定.除非我当然不能吃蛋糕而且也吃它.)
har*_*r07 10
似乎与BindingWinRT 类似,Binding在Windows Phone中,Universal Apps没有StringFormat属性.解决此限制的一种可能方法是使用本博Converter文中的解释,
要总结帖子,您可以创建一个IValueConverter接受字符串格式作为参数的文件:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的XAML中创建上述转换器的资源,然后您可以像这样使用它,例如:
<TextBlock x:Name="countTextBlock"
Text="{Binding Count,
Converter={StaticResource StringFormatConverter},
ConverterParameter='{}{0:n}'}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2695 次 |
| 最近记录: |