XAML 在WPF中使用C#在DataGrid中显示数据时出现问题

6bc51xsx  于 2022-12-20  发布在  C#
关注(0)|答案(1)|浏览(204)

我试图在Shortlist.xaml窗口的DataGrid中显示一些虚拟数据,但是,当我运行应用程序时,DataGrid为空。

Shortlist.xaml.cs中的代码如下所示:

namespace WpfApp_Employment_Help
{
    public partial class Shortlist : Window
    {
     
        public ObservableCollection<ShortlistedClient> clients { get; set; } = new ObservableCollection<ShortlistedClient>();

        public Shortlist()
        {
            InitializeComponent();
            DataContext = clients;
            clients.Add(new ShortlistedClient("Rich", "07111118265", "rich@gmail.com", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No"));
            clients.Add(new ShortlistedClient("Steve", "07567718265", "steve@gmail.com", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No"));
            clients.Add(new ShortlistedClient("Maria", "07485999005", "mb@gmail.com", "Edinburgh", "Office", "MSc", "more than 3 years", "No", "No"));
        }

        private void lstShort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dgr_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

My Shortlist.示例代码:

<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">
    <Grid>
        <DataGrid x:Name="dgr" d:ItemsSource="{d:SampleData ItemCount=5}" Margin="300,95,110,179" SelectionChanged="dgr_SelectionChanged"/>

    </Grid>
</Window>

My Client和ShortlistedClient类如下所示:

namespace WpfApp_Employment_Help
{
    public partial class Client

    {
        private static int nextID = 0;
        public string ID { get; private set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Location { get; }
        public string Worktype { get; }
        public string Qualification { get; }
        public string Workexp { get; }
        public string Drlicence { get; }
        public string Crconviction { get; }
        public bool IDed { get; private set; }

        public Client(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc)
        {
            Name = n;
            Phone = p;
            Email = e;
            Location = l;
            Worktype = wt;
            Qualification = q;
            Workexp = we;
            Drlicence = dl;
            Crconviction = cc;

        }

        public void AssignClientID()
        {
            if (!IDed)
            {
                ID = nextID.ToString("D4");
                nextID++;
                IDed = true;
            }
        }

 
    }
}

以及

namespace WpfApp_Employment_Help
{
    
    public class ShortlistedClient : Client
    {

        public DateTime DT { get; private set; }
        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;

        }
    }
}

在我的主窗口中,我有这个方法激活Shortlist.xaml.(因为它打开一个新窗口,Shortlist.xaml后,一个按钮点击):

private void GenShortlist_Click(object sender, RoutedEventArgs e)
        {
            Shortlist w = new Shortlist();
            w.Show();
        }

谁能告诉我出什么事了?

jv2fixgn

jv2fixgn1#

由于您要将窗口的DataContext设置为ObservableCollection<T>,因此应将DataGridItemsSource属性直接绑定到它,如下所示:

<DataGrid x:Name="dgr" d:ItemsSource="{d:SampleData ItemCount=5}"
    ItemsSource="{Binding}" Margin="300,95,110,179" SelectionChanged="dgr_SelectionChanged"/>

相关问题