Paste DateTime format into combobox

I have a Harvest_Base class where all DateTime formats are displayed,

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 a view class in which I have two combo boxes for starting and stopping. I want to do something bu that these comboBoxes should show me values ​​in the format "hh: mm tt".

My questions:

  • Is binding required here? If so, explain with the code in return.

  • If no binding is required, then what can I do to achieve this result?

0
source share
1 answer

Either you bind the combo box directly to DateTime and apply StringFormat to the binding, or bind to the string representing your DateTime in the correct format. You can 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 to your liking.

+1
source

Source: https://habr.com/ru/post/949285/


All Articles