Home > Blockchain >  Problem with displaying date only using DatePicker in WPF
Problem with displaying date only using DatePicker in WPF

Time:12-21

My code displays both date and time giving an error for this line

sc.DT = DatePick.SelectedDate;
Error   CS0029  Cannot implicitly convert type 'System.DateTime?' to 'System.DateOnly?' 

I have this class where I use DateOnly:

    public class ShortlistedClient : Client, INotifyPropertyChanged
    {
        private DateOnly? _dt;

        public DateOnly?  DT
        {
            get { return _dt; }
            set { _dt = value; NotifyPropertyChanged(); }
        }

        public bool InterestedinVac { get; private set; }

        public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();
        public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)
        {
            DT = new DateOnly();
            InterestedinVac = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

and this code for my .xaml window:

        List<ShortlistedClient> shlclients = new List<ShortlistedClient>();


        public Shortlist()
        {
            InitializeComponent();     
            DataContext = shlclients;
            shlclients.Add(new ShortlistedClient("Rich", "07515118265", "[email protected]", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient("Steve", "07515118265", "[email protected]", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient("Maria", "07485999005", "[email protected]", "Edinburgh", "Office", "MSc", "more than 3 years", "No", "No"));
        }


        // method to add date to each selected client
        private void addInterviewDT(object sender, RoutedEventArgs e)
        {
            ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;

            if (sc != null )
            {
                sc.DT = DatePick.SelectedDate;
            }

        }

I have tried changing DateOnly? to DateTime? in my ShortlistedClient class, and then set { _dt = value.ToShortDateString(); .. } but it gives an error as well.

and I have tried

private DateTime _dt;

        public DateTime  DT
        {
            get { return _dt; }
            set { _dt = value.Date; NotifyPropertyChanged(); }
        }

        public bool InterestedinVac { get; private set; }

        public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();
        public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)
        {
            DT = new DateTime(); 
            InterestedinVac = true; 
        }

which gives:

Error   CS0266  Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?) 

Finally I have tried

 <DataGridTextColumn Header="Interview Date" Binding="{Binding DT, StringFormat=\{0:dd.MM.yy\}}"/>

yet the error still persists.

What can I change here to avoid this error?

CodePudding user response:

The error messages say it all.

Error CS0029 Cannot implicitly convert type System.DateTime?' to 'System.DateOnly?'

So you need to convert the type explicitly.

For example:

DateTime dateTime = DateTime.Now;
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);

CodePudding user response:

Change the type of the DT property to DateTime?. Then this should work:

sc.DT = DatePick.SelectedDate;

If you still want to use a DateOnly, you should convert the selected date to a DateOnly before setting the property:

private void addInterviewDT(object sender, RoutedEventArgs e)
{
    ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;

    if (sc != null && DatePick.SelectedDate.HasValue)
    {
        sc.DT = DateOnly.FromDateTime(DatePick.SelectedDate.Value);
    }
}
  • Related