我有一个数据网格,看起来像这样。
我希望用户能够在"面试日期和时间"列中为选定项目单击"添加面试日期"后手动输入日期。
到目前为止,Shortlist.xaml.cs的代码如下所示:
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"));
}
// remove shortlisted client
private void RemoveShClient(object sender, RoutedEventArgs e)
{
if (dgr.Items.Count >= 1)
{
if (dgr.SelectedValue != null)
{
var items = (List<ShortlistedClient>)dgr.ItemsSource;
var item = (ShortlistedClient)dgr.SelectedValue;
dgr.ItemsSource = null;
dgr.Items.Clear();
items.Remove(item);
dgr.ItemsSource = items;
}
}
else
{
System.Windows.Forms.MessageBox.Show("No Clients Found");
}
}
所以我需要帮助来编写这个方法:
// method to fill in data for the date and time column
private void addInterviewDT(object sender, RoutedEventArgs e)
{
ShClient sc = dgr.SelectedItem as ShClient;
if (sc != null)
{
?
}
}
"我的客户端"和"ShortlistedClient"类定义如下:
一个二个一个一个
我的Shortlist.xaml代码是这样的:
<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="27,0,0,77">
<DataGrid x:Name="dgr" AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False" Height="154" Width="760">
<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="Interview Date and time" Binding="{Binding DT}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Interview DT" Width="128" FontWeight="Bold" Height="28"/>
<TextBox TextWrapping="Wrap" Text="TextBox" Width="120" Height="46"/>
<Button x:Name="BtnRemoveShlClient" Content="Remove Shortlisted Client" FontWeight="Bold" Height="33" Width="221" Click="RemoveShClient"/>
</StackPanel>
</Window>
此外,如何将面试日期和时间列值显示为空,而不是"1/1/0001 12:00:00 AM"?
谢谢,我对C#和wpf还不熟悉。
1条答案
按热度按时间91zkwejq1#
首先,
ShortlistedClient
必须实现INotifyPropertyChanged
,以便能够动态设置其属性之一,并自动更新UI:然后可以解析
TextBox
的值并尝试设置属性:textBox
是TextBox
控件的名称:另外,如何将面试日期和时间列值显示为空,而不是显示为“1/1/0001 12:00:00 AM”?
将
DT
属性的类型更改为Nullable<DateTime>
。