I wanna show a List in my DataGrid Unfortunately it doesn't show anything.
I know that their are many other threads with this topic but I couldn't apply them to my solution.
I tried several things:
ItemSource = "{Binding}"
<DataGrid x:Name="ContainerGrid" HorizontalAlignment="Left" Height="187" Margin="10,222,0,0" VerticalAlignment="Top" Width="772" IsReadOnly="True" AutoGenerateColumns="True" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTemplateColumn MinWidth="200" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding containers}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
(MainWindow.xaml.cs):
ContainerGrid.ItemsSource = Container.getContainerList();
Thats my current DataGrid:
<DataGrid x:Name="ContainerGrid" HorizontalAlignment="Left" Height="187" Margin="10,222,0,0" VerticalAlignment="Top" Width="772" IsReadOnly="True" AutoGenerateColumns="True" Grid.ColumnSpan="2" ItemsSource="{Binding containers}"/>
Thats my Class:
{
private string id;
private String name, version,status;
public static List<Container> containers = new List<Container>();
public Container() { }
public Container(string id,String containerName, String version,String status)
{
this.id = id;
name = containerName;
this.version = version;
this.status = status;
}
public static void AddContainerToList(Container container)
{
containers.Add(container);
}
public static List<Container> getContainerList()
{
return containers;
}
}
Thats how I add an object to the list:
Container.AddContainerToList(new Container(getID(line), getName(line), getVersion(line), getStatus(line)));
I don't see the mistake I did. Also read about ObservableCollections but that would be a big amount of changes to do in my opinion.
Please Help :D
p.s. sorry for my bad english
id
, name
, version
and status
must be defined as public properties for the DataGrid
to generate a column for them, and for you to be able to bind to them.
You probably also want to rename them to comply with the C# naming conventions that use PascalCase for property names:
public class Container
{
public string Id { get; private set; }
public string Name { get; private set; }
public string Version { get; private set; }
public string Status { get; private set; }
public static List<Container> containers = new List<Container>();
public Container() { }
public Container(string id, String containerName, String version, String status)
{
Id = id;
Name = containerName;
Version = version;
Status = status;
}
public static void AddContainerToList(Container container)
{
containers.Add(container);
}
public static List<Container> getContainerList()
{
return containers;
}
}