我试图格式化一个绑定到 TimeSpan
属性的文本块。如果属性的类型是 DateTime
,那么它可以工作,但是如果属性的类型是 TimeSpan
,那么它就会失败。我可以用转换器完成。但我想知道还有没有其他选择。
示例代码:
public TimeSpan MyTime { get; set; }
public Window2()
{
InitializeComponent();
MyTime = DateTime.Now.TimeOfDay;
DataContext = this;
}
赛姆
<TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>
我希望文本块只显示小时和分钟,但它显示如下:
19:10:46.8048860