I have a data-grid control and a series of controls that filter data in that data grid.
Those controls can be grouped into two as follows.
Today
, Yesterday
, Last Week
, Last Month
)From
and To
, when I clicked the button, the event takes values of those From
and To
date values and do the work)Now, I want to do some UX works as follows.
I can manage it works for requirement 2 but 1 is not going well. Below is my event handlers. I'm not using MVVM. Just with code-behind.
private void SelectionChanged_Filter(object sender, SelectionChangedEventArgs e)
{
/*DatePicker_FromDate.ClearValue(CalendarDatePicker.DateProperty);
DatePicker_FromDate.PlaceholderText = "Pick a Date";
DatePicker_ToDate.ClearValue(CalendarDatePicker.DateProperty);
DatePicker_ToDate.PlaceholderText = "Pick a Date";*/
DatePicker_FromDate.Date = null
DatePicker_ToDate.Date = null;
}
private void ButtonClick_Filter(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Clear the Filter Combobox's selected value
ComboBox_Filter.SelectedIndex = -1;
// EXCEPTION is in the below line:
var fromDate = Convert.ToDateTime(DatePicker_FromDate.Date.ToString()).ToString("dd-MM-yyyy");
var toDate = Convert.ToDateTime(DatePicker_ToDate.Date.ToString()).ToString("dd-MM-yyyy");
//...
}
The EXCEPTION is:
System.FormatException: 'String '' was not recognized as a valid DateTime.'
I think setting null
to date value of the calendar date picker is invalid. How can I clear those values when I changed the combobox?
Update: For clearing the value of CalendarDatePicker, you could set Date property to null like following.
DatePicker_FromDate.Date = null;
DatePicker_ToDate.Date= null;
For pass the value,first you could judge whether it is null then operate its value.
string toDate= = DatePicker_ToDate.Date == null ? "" : DatePicker_ToDate.Date.Value.ToString("dd/MM/yyyy");
If you are setting the Date
property to null
, the string
will be null
as well: using the following code that uses the safe-navigation operator ( ?.
):
var fromDate = DatePicker_FromDate.Date?.ToString("dd-MM-yyyy");
var toDate = DatePicker_ToDate.Date?.ToString("dd-MM-yyyy");
I guess this is what you want.
Also note that it's pointless to first convert a DateTime
to a string
just to convert it back to a DateTime
and then convert it to a string
again...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.