XAML 在WPF中仅使用DatePicker显示日期时出现问题

fgw7neuy  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(227)

我的代码同时显示日期和时间,并给出此行的错误

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

我有这样一个类,其中我使用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));
        }
    }

还有这个.xaml窗口的代码

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

        }

我尝试在ShortlistedClient类中将DateOnly?更改为DateTime?,然后设置{ _dt = value.ToShortDateString();..},但它也给出了错误。
我也试过

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

其给出:

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

我终于试过了

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

然而错误仍然存在。
我可以在这里做什么更改以避免此错误?

xghobddn

xghobddn1#

错误消息说明了一切。
错误CS0029无法将类型System.DateTime?“隐式转换为”System.DateOnly?“
因此需要显式转换类型。
例如:

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

相关问题