I'm trying to create combobox inside DataGridTemplateColumn but it should contain different values depending on the row. Here is my code:
<dg:DataGridTemplateColumn x:Name ="NameColumn" Header="Player Name">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
SelectedValue="0"
DisplayMemberPath="FullName"
SelectedValuePath="Id"
ItemsSource="{Binding AllPlayers, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"/>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
AllPlayers will return different list after each call.
public List<Player> AllPlayers
{
get
{
counter = counter + 1;
Debug.Print("getting all players " + counter);
List<Player> lst = new List<Player>();
for (int i=0; i < 5; i++)
{
Player p = new Player();
p.Id = counter + i;
p.FullName = "Name " + counter + i;
lst.Add(p);
}
return lst;
}
}
For some reason the AllPlayers function is called for the first 39 rows and then the data is taken from previously created lists. I can see that from debug info (it stops printing after 39 calls). And also lists in comboboxes are not unique. I do not understand the logic behind this behavior. I need AllPlayers be called for every row.
Show your binding to the grid. I would make Players a Public property of the collection you bind to the grid. In the ctor for the list of 39+ build the AllPlayers there. Lets assume your list of 39+ is teams and has properties Name, Manager, City, Players. Even if you get the players built in the template they are not directly associated with the team (without walking the visual tree).