问题描述
我试图一个XAML的StringFormat适用于时间跨度
属性。
I'm attempting to apply a XAML StringFormat to a TimeSpan
property.
注意:此问题适用于的Windows Phone 。
有关WPF,正确的语法是:
For WPF, the correct syntax is:
Text="{Binding ElapsedTime,StringFormat={}{0:m\\:ss}}"
Silverlight的,它需要单引号:
For Silverlight, it needs single-quotes:
Text="{Binding ElapsedTime,StringFormat='{}{0:m\\:ss}'}"
但对于Windows Phone的,不管是什么我都试过了,输出总是完整的00:00:00.0000000。
But for Windows Phone, no matter what I have tried, the output is always the full "00:00:00.0000000".
-
的StringFormat ='m\\:SS'
-
的StringFormat ='{} {0:m\\:SS}
-
的StringFormat ='m\:SS'
-
的StringFormat = m\\:SS
StringFormat='m\\:ss'
StringFormat='{}{0:m\\:ss}'
StringFormat='m\:ss'
StringFormat=m\\:ss
有我莫名其妙地错过了正确的排列,或者这只是不可能因为某些原因在Windows Phone的?
Have I somehow missed the correct permutation, or is this just not possible for some reason in Windows Phone?
更新
我用了一个解决方法(回答以下),但仍希望古怪的解释,如果有人知道。 (我说怪诞,因为的StringFormat
与的DateTime
物业工作得很好,只是不能与时间跨度
的。)
I used a workaround (answer below), but would still like an explanation of the weirdness, if anyone knows. (I say "weirdness" because StringFormat
works perfectly well with DateTime
properties, just not with TimeSpan
ones.)
更新#2
从2010年 (的StringFormat = \ {0:h\\:mm\\ \\}
)的工作?
A very similar question from 2010. Maybe this is just an issue with older and/or "compact" versions of .Net. Or would this answer (StringFormat=\{0:h\\:mm\}
) work?
推荐答案
最后我用一种变通方法。
I ended up using a workaround.
<TextBlock Text="{Binding DurationStr}" />
通过在视图模型一个新的属性:
With a new property in the view-model:
public string DurationStr
{
get { return Duration.ToString(@"m\:ss"); }
}
public TimeSpan Duration
{
get { return _duration; }
set
{
_duration = value;
RaisePropertyChanged("Duration");
RaisePropertyChanged("DurationStr");
}
}
private TimeSpan _duration;
这篇关于在Windows Phone的XAML时间跨度字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!