मैं WPF में केवल डेटपिकर से तारीख कैसे प्राप्त करूं?


मेरे पास उपयोगकर्ता के लिए मैन्युअल रूप से एक तारीख चुनने के लिए एक DatePicker है, और यह तारीख मेरे डेटाग्रिड में चयनित आइटम के लिए एक मान के रूप में जाती है।

हालाँकि, उदाहरण के लिए, इसे ’23/01/2023 12:00:00 पूर्वाह्न’ के रूप में प्रदर्शित किया जाता है।
मैं चाहता हूं कि इसे ’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

आप डेटा बाइंडिंग में दिनांक प्रारूप सेट कर सकते हैं। उदाहरण के लिए:

एक्सएमएल
<DataGridTextColumn Binding="{StartDate, StringFormat=\{0:dd.MM.yy\}}" />

अद्यतन

कभी-कभी इन स्थितियों में, विशिष्ट सुविधाओं को अपने मुख्य प्रोजेक्ट में लागू करने से पहले उनका परीक्षण करने के लिए एक प्रोटोटाइप प्रोजेक्ट बनाना सहायक होता है। मैंने यहां आपके लिए एक त्वरित डेमो प्रोजेक्ट में यह किया है कि यह कैसे काम करता है:

1. डेटा मॉडल

सी#
public class Widget
{
    public string? Name { get; set; }
    public DateTime? Date { get; set; }
}

2. पीछे कोड

सी#
public partial class MainWindow : Window
{
    private Random random = new();
    public ObservableCollection<Widget> Widgets { get; set; } = new();

    public MainWindow()
    {
        CreateWidgets();
        InitializeComponent();
    }

    private void CreateWidgets()
    {
        for (int i = 0; i < 10; i++)
        {
            Widgets.Add(new()
            {
                Name = $"Widget {i}", 
                Date = DateTime.Now.AddDays(random.NextDouble() * 5)
            });
        }
    }
}

3. खिड़की:

एक्सएमएल
<Window x:Class="WpfDataGridDateColumnFormatting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        x:Name="Window"
        Title="MainWindow" Height="450" Width="800">

    <DataGrid DataContext="{Binding ElementName=Window}"
              ItemsSource="{Binding Widgets}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name"
                                Binding="{Binding Name}" />
            <DataGridTextColumn Header="Date"
                                Binding="{Binding Date,
                                          StringFormat=\{0:dd.MM.yy\}}" />
        </DataGrid.Columns>
    </DataGrid>
</Window>

आउटपुट ऊपर बताए अनुसार काम करता है।

समाधान 3

थोड़ा देर हो चुकी है, लेकिन समस्या ‘डीटी’ के बाद अल्पविराम के गायब होने की हो सकती है:

<datagridtextcolumn header="Interview Date" Binding="{Binding DT StringFormat=\{0:dd.MM.yy\}}" />

コメント

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