0

我正在制作一个联系信息程序,用户可以在其中将姓名、年龄和生日以及其他一些内容输入到文本框中。当您单击确定时,我希望将数据放入DataGrid.

首先我有我的,当您按下创建新联系人按钮时MainWindow,它会打开一个新窗口:CreateContact

CreateContact cc = new CreateContact();
cc.ShowDialog();

然后打开一个新窗口,您可以在其中输入您的姓名和年龄并选择出生日期。然后单击确定:

private void cmdOk_Click(object sender, RoutedEventArgs e)
{
     MainWindow m = new MainWindow();
     m.name = txtName.Text;
     m.age = txtAge.Text;
     m.birthDate = dpBirthdate.Text;
     m.ShowContacts()
}

这应该ShowContacts在我的运行我的方法MainWindow

public string name;
public string age;
public string birthDate;

public void ShowContacts()
{
    try
    {
         dgContacts.ItemsSource = LoadContactData();
    }
    catch (Exception e)
    {
         MessageBox.Show("" + e);
    }
}

private List<ContactData> LoadContactData()
{
    List<ContactData> list = new List<ContactData>();
    list.Add(new ContactData()
    {
        Name = name,
        Age = age,
        BirthDate = birthDate
    });            
    return list;
}


public class ContactData
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string BirthDate { get; set; }
}

但是什么也没发生,我在DataGrid. 我希望有人能告诉我什么是错的。我真的尝试过大量搜索导致DataGrid不显示数据的原因。

4

3 回答 3

0

好的,这是我在程序中使用的所有 xaml 和类。我真的希望有人能帮助判断,出了什么问题以及为什么它不会在数据网格中显示任何数据。

MainWindow.xaml:

 <Window x:Class="KontaktInfo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid AutoGenerateColumns="True" Name="dgContacts" HorizontalAlignment="Left" Margin="0,95,0,0" VerticalAlignment="Top" Height="225" Width="517"/>

    <Button x:Name="cmdCreateContact" Content="Opret Kontakt" HorizontalAlignment="Left" Margin="0,68,0,0" VerticalAlignment="Top" Width="80" Click="cmdCreateContact_Click"/>
    <Button x:Name="cmdDeleteContact" Content="Slet Kontakt" HorizontalAlignment="Left" Margin="85,68,0,0" VerticalAlignment="Top" Width="80"/>

</Grid>

主窗口.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KontaktInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string name;
        public string age;
        public string birthDate;

        public MainWindow()
        {
            InitializeComponent();   
        }

        private void cmdCreateContact_Click(object sender, RoutedEventArgs e)
        {
            CreateContact cc = new CreateContact();
            cc.ShowDialog();
        }

        public void ShowContacts()
        {
            try
            {
                dgContacts.ItemsSource = LoadContactData();
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }

        private List<ContactData> LoadContactData()
        {
            List<ContactData> list = new List<ContactData>();
            list.Add(new ContactData()
            {
                Name = name,
                Age = age,
                BirthDate = birthDate
            });

            return list;
        }

    }

public class ContactData
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string BirthDate { get; set; }
    }
}

CreateContact.xaml:

     <Window x:Class="KontaktInfo.CreateContact"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CreateContact" Height="403" Width="300">
    <Grid>
        <Label Content="Navn" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="10,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="272"/>
        <Label Content="Alder" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="10,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="272"/>
        <Label Content="Fødselsdag" HorizontalAlignment="Left" Margin="10,118,0,0" VerticalAlignment="Top"/>
        <Button Name="cmdOk" Content="OK" HorizontalAlignment="Left" Margin="127,341,0,0" VerticalAlignment="Top" Width="75" Click="cmdOk_Click"/>
        <Button Name="cmdCancel" Content="Annuller" HorizontalAlignment="Left" Margin="207,341,0,0" VerticalAlignment="Top" Width="75"/>
        <DatePicker Name="dpBirthdate" HorizontalAlignment="Left" Margin="10,144,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

CreateContact.cs:

     using System;
     using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KontaktInfo
{
    /// <summary>
    /// Interaction logic for CreateContact.xaml
    /// </summary>
    public partial class CreateContact : Window
    {
        public CreateContact()
        {
            InitializeComponent();
        }

        private void cmdOk_Click(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.name = txtName.Text;
            m.age = txtAge.Text;
            m.birthDate = dpBirthdate.Text;
            m.ShowContacts();



        }
    }
}
于 2013-05-12T09:17:27.007 回答
0

假设dgContacts是datagrid视图控件,则必须调用以下命令确保数据绑定

dgContacts.DataBind();

该过程与此处说明的过程类似。

如果您已经调试过代码,并且这个建议不起作用,请调试并发布是否绑定到控件的数据。

于 2013-05-11T11:59:01.487 回答
0

简短回答:当您更改 ShowContacts 中的 ItemsSource 时,您似乎没有触发PropertyChanged事件。

长答案:你真的应该研究 MVVM。您应该只创建一个维护 ContactData 列表的 ViewModel,并在 XAML 中绑定到该列表(还将您的联系人对话框数据绑定到 ContactData 对象):

var contact = new ContactData();
var cc = new CreateContact { Datacontext = contact };
cc.ShowDialog();

// ok button has been hit at this point,
// you can check for cancel if you want
(List<ContactData>)(dgContacts.ItemsSource).Add(contact);

// may still need to fire PropertyChanged here depending on binding
于 2013-05-11T12:08:48.987 回答