我正在为我的窗口创建一个数据网格,并尝试向其添加列,然后向其中添加包含数据的行。你知道我做错了吗?我知道这是一件小而简单的事,我不明白。有人能帮我吗?谢谢!我现在得到一个stackoverflow异常。
public partial class Window5 : Window
{
item items = new item();
public Window5()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataGridTextColumn FirstName = new DataGridTextColumn();
FirstName.Header = "First Name";
FirstName.Binding = new Binding("FirstName");
Datagrid.Columns.Add(FirstName);
DataGridTextColumn LastName = new DataGridTextColumn();
LastName.Header = "Last Name";
LastName.Binding = new Binding("LastName");
Datagrid.Columns.Add(LastName);
DataGridTextColumn Department = new DataGridTextColumn();
Department.Header = "Department";
Department.Binding = new Binding("Department");
Datagrid.Columns.Add(Department);
DataGridTextColumn Time = new DataGridTextColumn();
Time.Header = "Time";
Time.Binding = new Binding("Time");
Datagrid.Columns.Add(Time);
Datagrid.Items.Add(new item() { FirstName = "Joey", LastName = "Chang", Department = "Education" });
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
public partial class item
{
public string FirstName {get; set;}
public string LastName { get; set; }
public string Department { get; set; }
}
}
Windows5.xaml:
<Window x:Class="WpfApp2.Window5"
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:WpfApp2"
mc:Ignorable="d"
Title="Window5" Height="450" Width="800" Loaded="Window_Loaded" Closing="Window_Closing">
<DataGrid x:Name="Datagrid" HorizontalAlignment="Left" Height="422" Margin="0,0,0,-3" VerticalAlignment="Top" Width="790"/>
</Window>
答案 0 :(得分:0)
从项目类中取出GenerateItems方法并将其放在window5类中。 然后调用方法
您的item类可能引用了不同的DataGrid。