Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:55:35+00:00 2026-05-27T05:55:35+00:00

Thanks for the help in advance. First time posting here. I have included a

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:55:35+00:00Added an answer on May 27, 2026 at 5:55 am

    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.

        class MyApplication
        {
            ObservableCollection<Team> _Teams = new ObservableCollection<Team>();
            ContextMenu _TeritorySwitcher = new ContextMenu();
            public MyApplication()
            {
    
            }
    
            public void AddTeam()
            {
                _Teams.Add(new Team());
                _Teams.Add(new Team());
                _Teams.Add(new Team());
                _Teams.Add(new Team());
    
                foreach (Team t in _Teams)
                {
                    t.Territories.Add(new Territory());
                    t.Territories.Add(new Territory());
                    t.Territories.Add(new Territory());
                }
    
                SetContextMenu();
            }
    
            private void SetContextMenu()
            {
                HierarchicalDataTemplate _hdtTerritories = new HierarchicalDataTemplate();
                _hdtTerritories.DataType = typeof(Territory);
    
                HierarchicalDataTemplate _hdtTeams = new HierarchicalDataTemplate();
                _hdtTeams.DataType = typeof(Team);
    
                FrameworkElementFactory _TeamFactory = new FrameworkElementFactory(typeof(TreeViewItem));
                _TeamFactory.Name = "txtTeamInfo";
                _TeamFactory.SetBinding(TreeViewItem.HeaderProperty, new Binding("TeamProperty1"));
    
                FrameworkElementFactory _TerritoryFactory = new FrameworkElementFactory(typeof(TreeViewItem));
                _TerritoryFactory.Name = "txtTerritoryInfo";
                _TerritoryFactory.SetBinding(TreeViewItem.HeaderProperty, new Binding("TerritoryProperty1"));
    
    
                _hdtTeams.ItemsSource = new Binding("Territories");
    
                _hdtTeams.VisualTree = _TeamFactory;
                _hdtTerritories.VisualTree = _TerritoryFactory;
    
                _hdtTeams.ItemTemplate = _hdtTerritories;
    
                _TeritorySwitcher.ItemTemplate = _hdtTeams;
                _TeritorySwitcher.ItemsSource = this._Teams;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using c# .net. Thanks in advance for any help. I have searched
I have a show/hide toggle working well in multiple instances (thanks to help here
Thanks for your help! I'd like to output all companyName entries that have uploads
Its my first time asking here, i hope get help, and meanwhile, help you
( First time programming in PHP. Had some help. Need a bit more. )
First time posting here, so go easy on me. :) I am trying to
thanks in advance for your help. I am wondering if there is a (design)
Thanks in advance for your help experts. I want to be able to copy
Thanks to pbos help and its program (published here , for xoring a big
To anyone who can help, Thanks Okay, so I have a database holding a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.