How to get time only out of DateTime in wpf - Error CS0029?

Maria 71 Reputation points
2022-12-20T10:45:36.923+00:00

I have this method which is meant to parse and get the time that the user types in the textBox in WPF.
It should then add a string like '12:05' as a value to one of my selected items.

private void addInterviewTime(object sender, RoutedEventArgs e)  
        {  
            ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;  
  
  
            if (sc != null && DateTime.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))  
            {  
                sc.DT = result;  
            }  
  
        }  

It gives the following error for the line, sc.DT = result;

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

Previously, I have set my DT property like this in my ShortlistedClient class:

public class ShortlistedClient : 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<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;   
        }   

my Shortlist.zaml code is as follows:

<Window x:Class="WpfApp_Employment_Help.Shortlist"  
        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"  
        xmlns:local="clr-namespace:WpfApp_Employment_Help"  
        mc:Ignorable="d"  
        Title="Shortlist" Height="450" Width="800">  
    <StackPanel Margin="0,0,0,73">  
        <DataGrid x:Name="dgr" AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False" Height="154" Width="793">  
            <DataGrid.Columns>  
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />  
                <DataGridTextColumn Header="Email" Binding="{Binding Email}" />  
                <DataGridTextColumn Header="Phone" Binding="{Binding Phone}"/>  
                <DataGridTextColumn Header="Location" Binding="{Binding Location}"/>  
                <DataGridTextColumn Header="Worktype" Binding="{Binding Worktype}"/>  
                <DataGridTextColumn Header="Qualification" Binding="{Binding Qualification}"/>  
                <DataGridTextColumn Header="Workexp" Binding="{Binding Worktype}"/>  
                <DataGridTextColumn Header="Driving licence" Binding="{Binding Drlicence}"/>  
                <DataGridTextColumn Header="Criminal conviction" Binding="{Binding Crconviction}"/>  
                <DataGridTextColumn Header="Interested in vacancy" Binding="{Binding InterestedinVac}"/>  
                <DataGridTextColumn Header="Interview Date" Binding="{Binding DT}"/>  
            </DataGrid.Columns>  
        </DataGrid>  
        <Button Content="Add Interview DT" Width="128" FontWeight="Bold" Height="28" Click="addInterviewDT"/>  
        <DatePicker x:Name="DatePick" Height="52" Width="200"/>  
        <TextBox x:Name="textBox" TextWrapping="Wrap" Width="197" Height="45" Text="Enter time&#xD;&#xA;"/>  
        <Button x:Name="BtnRemoveShlClient" Content="Remove Shortlisted Client" FontWeight="Bold" Height="33" Width="220" Click="RemoveShClient"/>  
    </StackPanel>  
</Window>  

Can somebody please help me with this problem?

In addition, how can I set my default values for dates and times as null in the Interview Date/Interview Time columns, instead of '0001/01/01 12:00:00 AM' showing up?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,823 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
830 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.