Hello.
I have an app like this (
https://i.stack.imgur.com/cVUq1.jpg[
^]). I want the user to be able to add interview date with a DatePicker (DatePick) to enter it for a selected item in the Interview Date column, and then manually enter a time e.g. '11:115' into the Interview Time column by first selecting the item (each row) and then clicking the relevant buttons.
[![enter image description here][1]][1]
Right now it enters the date correctly displaying the date part only in the Interview Date column. however, when I enter the time, it changes Interview date for today's date (20/12/2022).
My code for a .xaml.cs:
public partial class Shortlist : Window
{
List<ShortlistedClient> shlclients = new List<ShortlistedClient>();
public DateTime? SelectedDateFormat { get; private set; }
public Shortlist()
{
InitializeComponent();
DataContext = shlclients;
shlclients.Add(new ShortlistedClient("Rich", "07515118265", "rich@gmail.com", "Glasgow", "Office", "BSc", "more than 3 years", "Yes", "No"));
shlclients.Add(new ShortlistedClient("Steve", "07515118265", "steve@gmail.com", "Glasgow", "Construction", "High School", "1-3 years", "Yes", "No"));
shlclients.Add(new ShortlistedClient("Simon", "07485999005", "simon@gmail.com", "Edinburgh", "Office", "MSc", "more than 3 years", "Yes", "No"));
}
private void addInterviewDate(object sender, RoutedEventArgs e)
{
ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;
if (sc != null)
{
sc.DT = DatePick.SelectedDate;
}
}
private void addInterviewTime(object sender, RoutedEventArgs e)
{
ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;
if (sc != null && DateTime.TryParse(textBox.Text, out DateTime result))
{
sc.DT = result;
}
}
and my .xaml window code is this:
```
<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 Workexp}"/>
<DataGridTextColumn Header="Driving licence" Binding="{Binding Drlicence}"/>
<DataGridTextColumn Header="Criminal conviction" Binding="{Binding Crconviction}"/>
<DataGridTextColumn Header="Interested in vacancy" Binding="{Binding InterestedinVac}"/>
<DataGridTemplateColumn Header="Interview Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DT, StringFormat=dd/MM/yyyy}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DT, StringFormat=dd/MM/yyyy}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Interview Time">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DT, StringFormat=HH:mm}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Interview Date" Width="128" FontWeight="Bold" Height="28" Click="addInterviewDate"/>
<DatePicker x:Name="DatePick" Height="52" Width="200"/>
<Button Content="Add Interview Time" FontWeight="Bold" Click="addInterviewTime" Height="25" Width="116"/>
<TextBox x:Name="textBox" TextWrapping="Wrap" Width="197" Height="45" Text="Enter time"/>
<Button x:Name="BtnRemoveShlClient" Content="Remove Shortlisted Client" FontWeight="Bold" Height="33" Width="220" Click="RemoveShClient"/>
</StackPanel>
</Window>
Earlier, my ShortlistedClient class and the date time variable was defined like this:
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;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
how can I make it work? I could make date and time as an entry in one column, but I want them to be in two separate columns.
[1]: https://i.stack.imgur.com/cVUq1.jpg
What I have tried:
I have tried to change my line for the time method like this. It does not give me any errors but now it stops taking the time entries.
it looks like the DatePicker dominates both entries because if I dont pick a particular date with the DatePicker, and click the Add Interview Date button, it clears the values for both Interview Date and Interview Time.
<pre lang="C#">```
private void addInterviewTime(object sender, RoutedEventArgs e)
{
ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;
if (sc != null && DateTime.TryParseExact(textBox.Text, "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result))
{
sc.DT = result;
}
}