简体   繁体   中英

Problem with displaying date only using DatePicker in WPF

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", "rich@gmail.com", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient("Steve", "07515118265", "steve@gmail.com", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient("Maria", "07485999005", "mb@gmail.com", "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?

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);

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);
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM