在组合框中插入DateTime格式 [英] Insert DateTime format in combobox

查看:138
本文介绍了在组合框中插入DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在那里所有的DateTime格式见Harvest_Base类;

 类Harvest_Base
{    公共静态的DateTime storeTime(字符串日期)
    {        日期时间的returnValue =新的DateTime();        如果(日期==)
            返回的returnValue;            //时间或日期组件不存在
            字符串[] =格式{M / D / YYYY H:MM:SS TT,M / D / YYYY H:毫米TT
               MM / DD / YYYY HH:MM:SS,M / D / YYYY H:MM:SS
               M / D / YYYY HH:MM TT,M / D / YYYY HH TT,M / D / YYYY时:分,M / D / YYYY时:分,
               MM / DD / YYYY HH:MM,M / DD / YYYY HH:MM,
               H:MM TT,HH:MM TT,HH:MM:SS,H:MM,HH:MM,H:mmtt};
            DateTime的结果;            如果(DateTime.TryParseExact(日期,格式,System.Globalization.CultureInfo.InvariantCulture,DateTimeStyles.None,出结果))
                的returnValue =结果;
            其他
                的returnValue = DateTime.Today;        返回的returnValue;    }}

我有我已经2组合框的开始时间和停止时间视图类。我想要做的事BU这些组合框应显示在我的HH:MM TT值格式。

我的问题是:


  1. 是绑定在这里需要?如果有,请回答code解释。


  2. 如果绑定不需要那么我能做些什么来实现这个结果?



解决方案

要么你直接绑定组合框的的DateTime 并应用的StringFormat 的绑定,或绑定到一个字符串,再$ p $以正确的格式psenting你的的DateTime 。你也可以使用一个值转换器,但它是一个有点矫枉过正。

这里的 的StringFormat 绑定子句中

  {绑定路径= PathToTheDateTime,的StringFormat = {} {0:MM-DD-YYYY}}

更改 MM-DD-YYYY 部分根据自己的喜好。

I've Harvest_Base class where all DateTime formats are shown;

class Harvest_Base
{

    public static DateTime storeTime(String date)
    {

        DateTime returnValue = new DateTime();

        if (date == "")
            return returnValue;

            //Time or Date Component Does not Exist
            string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
               "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
               "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
               "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm", 
               "h:mm tt","hh:mm tt","HH:mm:ss","H:mm","HH:mm","h:mmtt"};
            DateTime result;

            if (DateTime.TryParseExact(date, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
                returnValue = result;
            else
                returnValue = DateTime.Today;

        return returnValue;

    }

}

I have view class in which I've two comboboxes for starttime and stoptime. I want to do something bu which these comboBoxes should show me values in "hh:mm tt" format.

My questions are:

  1. Is binding required here? If yes, please explain with code in answer.

  2. If binding not required then what can I do to achieve this result?

解决方案

Either you bind the combo box directly to the DateTime and apply a StringFormat on the binding, or you bind to a string representing your DateTime in the proper format. You could also use a value converter, but it's a bit overkill.

Here's the StringFormat in the Binding clause

{Binding Path=PathToTheDateTime, StringFormat={}{0:MM-dd-yyyy}}

Change the MM-dd-yyyy part to your liking.

这篇关于在组合框中插入DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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