Thanks for the help in advance. First time posting here.
I have included a bit of sample code. I would like to dynamically build a ContextMenu based on these custom objects(ObservableCollection). I can bind the ContextMenu to the first level of Team, but can you also bind a second level “ContextMenu? / MenuItem?” for the territories. I need to see the territories in a team when the team is highlighted.
My Team Object
class Team
{
private int _TeamProperty1 = 0;
private int _TeamProperty2 = 0;
ObservableCollection<Territory> _Territories = new ObservableCollection<Territory>();
public Team()
{
}
public ObservableCollection<Territory> Territories
{
get { return _Territories; }
set { _Territories = value; }
}
public int TeamProperty1
{
get { return _TeamProperty1; }
set { _TeamProperty1 = value; }
}
public int TeamProperty2
{
get { return _TeamProperty2; }
set { _TeamProperty2 = value; }
}
}
My Territory Object
class Territory
{
private int _TerritoryProperty1 = 0;
public int TerritoryProperty1
{
get { return _TerritoryProperty1; }
set { _TerritoryProperty1 = value; }
}
public void Method1()
{
//Do Some Work
}
}
Application
class MyApplication
{
ObservableCollection<Team> _Teams = new ObservableCollection<Team>();
ContextMenu _TeritorySwitcher = new ContextMenu();
public MyApplication()
{
AddTeam()
}
public void AddTeam()
{
Team _Team1 = new Team();
_Team1.Territories.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Territories_CollectionChanged);
_Teams.Add(_Team1);
Team _Team2 = new Team();
_Team2.Territories.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Territories_CollectionChanged);
_Teams.Add(_Team2);
Team _Team3 = new Team();
_Team3.Territories.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Territories_CollectionChanged);
_Teams.Add(_Team3);
foreach (Team t in _Teams)
{
t.Territories.Add(new Territory());
t.Territories.Add(new Territory());
t.Territories.Add(new Territory());
t.Territories.Add(new Territory());
}
}
void Territories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ObservableCollection<Team> _TeamsSort = new ObservableCollection<Team>(_Teams.OrderBy(tm => tm.TeamProperty1));
_TeritorySwitcher.ItemsSource = _TeamsSort;
_TeritorySwitcher.DisplayMemberPath = "TeamProperty2";
}
}
Now my ContextMenu shows the teams (3 of them), but I can’t figure out how to also show the Territories (There should be 4 in each team)
Ok, I figured it out. Thanks for the direction. Here is the new MyApplication Class. In the sample I don’t have any data but you can fill that in if you need to see this work. It’s just a sample framework.