好的,这是我在程序中使用的所有 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();
}
}
}