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 957963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:47:23+00:00 2026-05-16T00:47:23+00:00

Background I have a TreeView that follows the MVVM design pattern and supports multiple

  • 0

Background

I have a TreeView that follows the MVVM design pattern and supports multiple selection by recording TreeViewItem selections in a List. As it stands there are several types of TreeViewItems available for the user to select.

They are:

  • Two Root nodes of type WorldFolder and MyDataFodler which can contain child Folder types
  • Child Folder nodes of types LocationFolder, PersonFolder, CollectionFolder
  • Child Item nodes of type LocationItem, PersonItem
  • CollectionFolder can contain child nodes of Folder types

In all this works very well with very little code and supports collections of Locations and People and furthermore Collections within Collections.

Problem / Question

My top level view-model keeps track of the selection state of TreeViewItems and the current selection may be a combination of Item, Folder or even Root type nodes. Depending on the user’s selection I want to create a dynamic ContextMenu. So far this works! When I select several LocationItem and/or PersonItem type nodes my view-model generates a custom ContextMenu. The problem is the complexity! My view-model is quickly turning into dozens of if/else if/else statements to capture all the possible permutations!

For example:

if (_selectedItems.All(item => item is PersonItem)) // Only people selected
{ 
  // Create ContextMenu based on only PersonItems 
}
else if( _selectedItems.All(item => item is LocationItem)) // Only Locations
{
  // Create ContextMenu based only on LocationItems
}
...

Is there a better way to handle all the possible permutations of user choices and generate my ContextMenus more efficiently?

* Sorry about the code formatting, it’s been giving me grief all week *

  • 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-16T00:47:24+00:00Added an answer on May 16, 2026 at 12:47 am

    I can’t remember where I read this wise saying:
    The best way to work with a TreeView is not not to work with the TreeView

    What does this mean? Move the functionality into the tree nodes and keep the tree view as thumb as possible. Unfortunately, by default the tree node does not many events, though, it’s easy to redirect the tree view events to the nodes.

    Once done, you can override the ContextMenuStrip property in your nodes. The first selected node creates the list of ToolStripItems it wants to handle and asks the tree view which are allowed (e.g. with a FilterMenuItems(desiredItems) method). The tree view asks all selected nodes which of the nodes they would be able to handle. The result is your context menu.

    This should work with almost any count of different nodes and keeps the tree (nodes) easy to maintain.

    Edit: Dang! Missed the WPF tag, so I cannot asses the available events, since I did not yet work with WPF

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
Background: we have an application that generates reports from HTML (that may or may
In my application, I have a TreeView that allows drag/drop. I have all the
I have a TreeView control showing multiple TreeNodes in an organised heirarchy. I want
Background: I have a custom user control based around a WPF TreeView. I've been
I have a tree view control that I would like to supply the background
Background I have a web app that will create an image from user input.
Background: I have a little video playing app with a UI inspired by the
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I have been asked by a client to create a picture of the

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.