XAML 如何为WPF中的选定项向数据网格添加列?

jv2fixgn  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(145)

我有一个数据网格,看起来像这样。

我希望用户能够在"面试日期和时间"列中为选定项目单击"添加面试日期"后手动输入日期。
到目前为止,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还不熟悉。

91zkwejq

91zkwejq1#

首先,ShortlistedClient必须实现INotifyPropertyChanged,以便能够动态设置其属性之一,并自动更新UI:

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 = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后可以解析TextBox的值并尝试设置属性:

private void addInterviewDT(object sender, RoutedEventArgs e)
{
    ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;

    if (sc != null && DateTime.TryParse(textBox.Text, out DateTime value))
    {
        sc.DT = value;
    }
}

textBoxTextBox控件的名称:

<TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Width="120" Height="46"/>

另外,如何将面试日期和时间列值显示为空,而不是显示为“1/1/0001 12:00:00 AM”?
DT属性的类型更改为Nullable<DateTime>

相关问题