HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Display submenus

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
displaysubmenusstackoverflow

Problem

I am displaying menus and submenus in my WinRT app, and I think I am doing it wrong. I have thought I was doing it wrong for a long time, so here it is for review.

This is the section in my ViewModel. Probably needless to say, both Remove() and NewSelectionWS() have had large quantities of nearly identical code removed; feel free to comment on everything:

private ObservableCollection _itemList = new ObservableCollection();
public ObservableCollection ItemList
{
get { return _itemList; }
set
{
_itemList = value;
OnPropertyChanged("ItemList");
}
}

private void Remove()
{
foreach (string s in GlobalVars.OneNote()) { ItemList.Remove(s); }
foreach (string s in GlobalVars.MainMenu()) { ItemList.Remove(s); }

foreach (Type t in GlobalVars.OneNoteP()) { Pages.Remove(t); }
foreach (Type t in GlobalVars.MainMenuP()) { Pages.Remove(t); }
}

public void NewSelectionWS(string val)
{
if (!val.StartsWith(" ")) { Remove(); }

switch (val)
{
case "Menu 1":
foreach (string s in GlobalVars.OneNote()) { ItemList.Insert(ItemList.IndexOf("Menu 1") + 1, s); }
foreach (Type t in GlobalVars.OneNoteP()) { Pages.Insert(ItemList.IndexOf("Menu 1") + 1, t); }
break;

case "Menu 2":
foreach (string s in GlobalVars.MainMenu()) { ItemList.Insert(ItemList.IndexOf("Menu 2") + 1, s); }
foreach (Type t in GlobalVars.MainMenuP()) { Pages.Insert(ItemList.IndexOf("Menu 2") + 1, t); }
break;
}
}


This is GlobalVars:

`private static string[] _homeWS = { "Menu 1", "Menu 2", "Menu 3", "Menu 4" };
private static string[] _oneNote = { " Submenu 1", " Submenu 2", " Submenu 3" };
private static string[] _mainMenu = { " Submenu 1", " Submenu 2", " Submenu 3" };

public static string[] HomeWS() { return _homeWS; };
public static string[] OneNote() { return _oneNote; }
public static string[] MainMenu() { return _mainMenu; }

private static Ty

Solution

Ok now I see the whole picture.
So my comment to your code is that your idea to workaround lack of TreeView (which, by the way, can be customized to be always expanded and without +- icons) is pretty good, but the way you approach is not. Generally what you are doing wrong is that you mix menu logic with UI. To be concrete:

-
Create a class that represents a menu item, give it properties like name (without spaces), depth level, add a collection of children menu items. Make the class inherit form INotifyPropertyChanged interface to provide fully-capable view model class (or just skip it if you don't want to modyfi your menus at runtime or replace always whole tree).
Don't put any UI code in this view model.

-
Create user control with dependency property that you bind to root menu. In the code behind fill your list view depending on the view model, calculate number of spaced dynamically depending on menu item depth.

This'll make your solution very robust and extendable. By adding new properties to menu item view model you'll be able to easily customize each item.

Context

StackExchange Code Review Q#75163, answer score: 2

Revisions (0)

No revisions yet.