I have this datagrid and in the lines where there a data in there are also data in the combobox but when I want to add a new line the combobox is empty. Is there any way to get around it so there is data when I add a new line in the datagrid?
<DataGrid x:Name="po" ItemsSource="{Binding dækning}" Grid.Column="9" Grid.Row="1" Grid.RowSpan="3"
Grid.ColumnSpan="3" AutoGenerateColumns="False" AddingNewItem="Bladdaeking_AddingNewItem"
CellEditEnding="Bladdaeking_CellEditEnding">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Post nr">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Postby}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate >
<ComboBox ItemsSource="{Binding TblPostNrs}"
DisplayMemberPath="PostNr" ></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Dækning">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DaekningsGrad1}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Post by">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Postby}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Oplag">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
This is the object that the DataGrid takes in
public class Bladdaeknik
{
private int BladID;
private int Oplag;
private int DaekningsGrad;
private int PostNr;
private string postby;
public int DaekningsGrad1 { get => DaekningsGrad; set => DaekningsGrad = value; }
public int PostNr1 { get => PostNr; set => PostNr = value; }
public int Oplag1 { get => Oplag; set => Oplag = value; }
public int BladID1 { get => BladID; set => BladID = value; }
public string Postby { get => postby; set => postby = value; }
public List<daos.PostNrModel> TblPostNrs { get; set; }
}
And the code in the *.xaml.cs page
private void AddBladDaeknignToDataGrid(int stamBladid)
{
var res = BladDækningDao.GetDækningGradByBladId(stamBladid);
res.Add(new Bladdaeknik() {
BladID1 = stamBlad.BladID
});
po.DataContext = new { dækning = res };
da.DataSource = BladDækningDao.GetDækningGradByBladId(stamBladid);
}
Daekning is a collection of Bladdaeknik.
The datacontext of each row is therefore one of those Bladdaeknik.
When you append, this is also true. A new instance of one of these Bladdaeknik will be created.
The datacontext of that combo in the new row is your new Bladdaeknik.
If you want some TblPostNrs to show as options then you need to set them up in the constructor of Bladdaeknik for a new instance. For ones you're editing you can go with that or replace them with some others since you can translate your data from database into that viewmodel.
Or bind to a collection elsewhere using a resource or relativesource or some such to provide one list of these for all rows.
Depending on your requirement.
public class Bladdaeknik
{
public Bladdaeknik()
{
// Fill this.TblPostNrs with data
}
( Apologies if I mis-spelt anything there ).