I have nested ObsevableCollections as seen below. For simplistic reason this is sample code of what I am trying to accomplish.
Sample application the contains a ObservableCollection Team
class MyApplication2
{
ObservableCollection<Team> _Teams = new ObservableCollection<Team>();
public MyApplication2()
{
_Teams.Add(new Team("Team1"));
_Teams.Add(new Team("Team2"));
_Teams.Add(new Team("Team3"));
foreach (Team t in _Teams)
{
t.Territories.Add(new Territory("Territory1"));
t.Territories.Add(new Territory("Territory2"));
t.Territories.Add(new Territory("Territory3"));
}
}
}
Team object that contains an ObservableCollection Territory object
class Team
{
private string _TeamName = "";
private int _TeamProperty1 = 0;
ObservableCollection<Territory> _Territories = new ObservableCollection<Territory>();
public Team(string tName)
{
this.TeamName = tName;
}
public ObservableCollection<Territory> Territories
{
get { return _Territories; }
set { _Territories = value; }
}
public string TeamName
{
get { return _TeamName; }
set { _TeamName = value; }
}
public int TeamProperty1
{
get { return _TeamProperty1; }
set { _TeamProperty1 = value; }
}
}
Territory object
class Territory
{
private string _TerritoryName = "";
public Territory(string tName)
{
this.TerritoryName = tName;
}
public string TerritoryName
{
get { return _TerritoryName; }
set { _TerritoryName = value; }
}
public void Method1()
{
//Do Some Work
}
}
Just for this example I assigned t a value but in my real code I have the object t from some other means. t is an object type of territory in an ObservableCollection of Territories inside and object of a Team.
public void SomeWork()
{
Territory t = _Teams[1].Territories[0];
SomeMoreWork(t);
}
I am removing a territory from 1 team and adding it to another team. Something like the following. How I get what Team object the territory belongs to?
public void SomeMoreWork(Territory t)
{
Team _Team = ( Parent of t? );
_Team.Territories.Remove(t);
_Teams[0].Territories.Add(t);
}
Thank you for the info Vincent Piel.
Thank you for the idea Saad Imran.
I changed the following and added an AssociatedTeam to my territory object and set it in the consructor. So far it seems to do what Im looking for.
And then change the initiation of territory in the constructor of MyApplication