绑定中的Datetime对象的StringFormat返回小时和分钟的0 [英] StringFormat of Datetime object in binding gives back 0 for hour and minute

查看:190
本文介绍了绑定中的Datetime对象的StringFormat返回小时和分钟的0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Datetime创建一个Datetime对象.现在,我将此作为类的属性.

I create a Datetime object with Datetime.Now and I have this as a property of a class.

将其绑定到网格视图时:

When I bind this to a grid view:

<GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding transaction_date1, StringFormat=HH:mm}" />

结果始终为00:00.

The result is always 00:00.

当我调试代码时,我看到Datetime对象的小时,分​​钟等属性包含非零值. 我认为在这种情况下,StringFormat从具有正确日期但始终始终为12:00:00 AM的Datetime对象中的Date对象获取小时和分钟.

When I debug the code I see that the hour, minute etc. properties of the Datetime object contain non-zero values. I think that the StringFormat in this case gets the hour and minute from the Date object within the Datetime object that has the correct dates but always has 12:00:00 AM as hour.

有什么办法可以超越它,并在我的窗口中显示正确的小时和分钟?

Is there any way to go past it and display the right hour and minute in my window?

提前谢谢!

推荐答案

将其更改为此:

<GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding transaction_date1, StringFormat='{}{0:HH:mm}'}" />

如果要使用StringFormat进行自定义格式设置,则必须使用此方法在格式字符串中提供参数索引的位置.等效于:

If you want to use StringFormat with custom formatting, then you have to use this method where you provide the param index in the format string. It is equivalent to:

string.Format("{0:HH:mm}", transaction_date1);

{}开头的两个大括号是XAML解析器的指令,可忽略字符串中发现的其他大括号.因此,您可以在一个绑定语句中多次使用日期值:

The two curly braces at the start {} are an instruction to the XAML parser to ignore further curly braces found in the string. So you could use your date value multiple times in one binding statement:

DisplayMemberBinding="{Binding transaction_date1, StringFormat=`{} Your date-time is {0:dd/mm/yy} at approx. {0:HH} hours and {0:mm} minutes`}"

这篇关于绑定中的Datetime对象的StringFormat返回小时和分钟的0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆