【解決方法】WPFでdatepickerからのみ日付を取得するにはどうすればよいですか?


ユーザーが日付を手動で選択するための DatePicker があり、この日付はデータグリッドで選択した項目の値として入力されます。

ただし、たとえば、「23/01/2023 12:00:00 AM」と表示されます。
「23/01/2023」と表示させたいです。

私は次のように私のコードを持っています:

    public partial class Shortlist : Window
    {
     
//        public ObservableCollection<ShortlistedClient> shlclients { get; set; } = new ObservableCollection<ShortlistedClient>();
        List<ShortlistedClient1> shlclients = new List<ShortlistedClient1>();

        public DateTime? SelectedDateFormat { get; private set; }

        public Shortlist()
        {
            InitializeComponent();
        //    createShClientList();
            DataContext = shlclients;
            shlclients.Add(new ShortlistedClient1("Rich", "07515118265", "rich@gmail.com", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient1("Steve", "07515118265", "steve@gmail.com", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No"));
            shlclients.Add(new ShortlistedClient1("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;
            }

次に、私のクラス定義は次のようになります。

public class ShortlistedClient1 : Client, INotifyPropertyChanged
{
    private DateTime? _dt;

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

    public bool InterestedinVac { get; private set; }


    public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient1>();
    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;
    }

私が試したこと:

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

解決策 1

データ バインディングで日付形式を設定できます。 例えば:

XML
<DataGridTextColumn Binding="{StartDate, StringFormat=\{0:dd.MM.yy\}}" />

コメント

タイトルとURLをコピーしました