简体   繁体   中英

My data is not displayed in the datagrid

dg.ItemsSource=GetList();

I debug the program and see that ItemSource receives full items but there is nothing shown in the grid at all. I also would like to know how can I dock my datagrid control into the WIndows such that it also resizes itself once I resize the parent windows? Thank you

{ UPDATE }

I am wpf new learner. I think using only the source code above can also automatically bind the data source with the specified control. I am not thinking about creating a loop to insert item by item into the grid. I will do this but I need you help me tell me whether what I am thinking is correct.

My datalist is a list of class that contains string items

    public class Author
    {
        public string Name { get; set; }
        public DateTime PostedDate { get; set; }
        public string ProjectTitle { get; set; }
        public string Content { get; set; }
        public string Link { get; set; }
    }

Check the AutoGenerateColumns, Height, Width, HorizontalAlignment and VerticalAlignment properties on the grid. If you just drag-and-drop it from the Toolbox onto your XAML design surface, this code gets generated:

<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="254,64,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />

Changing it to this:

<DataGrid HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch"/>

should solve both of your problems.

EDIT: You did not specify what type of elements your list contains, but be aware that the automatically generated columns will be bound to the public properties of your list items.

EDIT2: Now that you added your list item type, here's a sample:

MainWindow.xaml.cs:

public partial class MainWindow: Window
{
  public MainWindow()
  {
    InitializeComponent();

    List<Author> list = new List<Author> 
    {
      new Author { Name = "X Y", Content = "blah" },
      new Author { Name = "W Z", Content = "blah blah" },
      new Author { Name = "N N", Content = "blah blah blah" },
      new Author { Name = "M M", Content = "blah blah blah blah" },
    };
    dataGrid1.AutoGenerateColumns = true;
    dataGrid1.ItemsSource = list;
  }
}

public class Author
{
  public string Name { get; set; }
  public DateTime PostedDate { get; set; }
  public string ProjectTitle { get; set; }
  public string Content { get; set; }
  public string Link { get; set; }
}

MainWindow.xaml:

<Window x:Class="WpfApplication1.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 HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch"/>
  </Grid>
</Window>

Output:

样品输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM