ContextMenu in Windows Phone 7 - c#

How to create several MenuItems and add it to ContextMenu in windows phone 7 with C#?
I wrote this sample:
MenuItem q = new MenuItem();
q.Header = "something";
ContextMenu cM = new ContextMenu(q);
and in MSDN there's something about it, but it doesn't compile, because of construct of ContextMenu.

You can add menu items as follows:
ContextMenu cm = new ContextMenu();
cm.Items.Add( new MenuItem() {
Header = "Item 1",
} );

The ContextMenu isn't part of Wp7 natively. You need to look at the Silverlight Toolkit for WP7.
http://silverlight.codeplex.com/

Related

How to perform nested ContextMenu implementation?

I have this menu like this:
// open context menu
var contextMenu = new ContextMenu();
var versionsMenu = new ToolStripDropDownMenu() {Text = "Version"};
StaticHelpers.GetPackageVersions(textBlockSelected.Text).ForEach(f=> versionsMenu.Items.Add(f));
var scheduleMenu = new ToolStripMenuItem {Text = "Schedule"};
var argumentsMenu = new ToolStripMenuItem() {Text = "Arguments"};
var removeMenu = new ToolStripMenuItem {Text = "Remove"};
//show context menu
contextMenu.Items.Add(versionsMenu);
contextMenu.Items.Add(scheduleMenu);
contextMenu.Items.Add(argumentsMenu);
contextMenu.Items.Add(removeMenu);
//add handlers
// executeMenu.Click += (o, args) => { ExecutePackage(sender); };
//open context menu
contextMenu.IsOpen = true;
I have tried MenuItems instead of ToolStripMenuItem or ToolStripDropDownMenu but could not find any documentation or examples anywhere about how you can make these things nested, for example, when the user right clicks on one of my controls I want to show this menu:
Version
Schedule
Arguments
Remove
If the user hovers over version, I want another contextMenu to extend and show the following:
V1.0
V1.1
V1.2
How can I achieve this functionality?
To add a sub-menu, you take an existing item and do the same to it:
var versionsMenu = new ToolStripMenuItem();
versionsMenu.DropDownItems.Add(nestedItem);

Xamarin Forms ListView ItemsSource issue

I have a ListView and its ItemsSource.
ListView IList = new ListView();
IList.ItemsSource = _routine_names;
In order to customize the Data Template for each item I'm doing:
IList.ItemTemplate = new DataTemplate(()=>
{
Label Routine_Name = new Label(); // should be_routine_names
return new ViewCell
{
View = new StackLayout{
Children = {Routine_Name}
}
};
});
When I run this my ListView is displayed and the list items are indeed there (they have onclick handlers that work) but there is no text, which should be whatever is in _routine_names.
My question how do I get the Label in the DataTemplate to be items in _routine_names?
The reason I'm adding a DataTemplate is so I can add swipe to delete.
You can just use the built in TextCell for what you're trying to do. It has a bindable TextProperty and an optional DetailProperty if you want a smaller text line below the main one.
IList.ItempTemplate = new DataTemplate(()=>
{
var textCell = new TextCell();
textCell.ContextActions.Add(yourAction);
textCell.SetBinding(TextCell.TextProperty, ".");
return textCell;
}
IList.ItemsSource = _routine_names;
yourAction is of type MenuItem as seen here
Also, please notice that IList is a name of a class in System.Collection namespace so I'd use something else there.

How to add submenu items to menuitems

I have created a Table layout in windows forms as shown in figure, i have added a right mouse button click Menu to my table,
i wnt to add submenu items to following menuitems such as
When i click add device it should show subitems such as sounder, MCP, strobe etc
When i click fault it should show subitems such as open circuit fault, sounder fault, `short circuit fault' so on
This is how i created menuitems
ContextMenu m = new ContextMenu();
MenuItem addDevice = new MenuItem("Add Device");
MenuItem deleteDevice = new MenuItem("delete Device");
MenuItem fire = new MenuItem("Fire");
MenuItem fault = new MenuItem("Fault");
MenuItem suppress = new MenuItem("Suppress");
m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault,suppress});
tableLayout.ContextMenu = m;
m.Show((Control)(sender), e.Location);
The below is my form and table layout
You can add MenuItems to existing MenuItem like:
MenuItem addDevice = new MenuItem("Add Device");
addDevice.MenuItems.Add( new MenuItem("Add More .."));
It would be visible like:
MenuItem newMenuItem1 = new MenuItem("Employee Master");
MenuItem mnuSubMenuItem = new MenuItem();
newMenuItem1.ChildItems.Add(mnuSubMenuItem);
mnuSubMenuItem.Text = "Add User...";
mnuSubMenuItem.NavigateUrl = "ADDURL.aspx";
MenuItem mnuSubMenuItem1 = new MenuItem();
newMenuItem1.ChildItems.Add(mnuSubMenuItem1);
mnuSubMenuItem1.Text = "Edit User...";
mnuSubMenuItem1.NavigateUrl = "EDITURL.aspx";
this.MenuMaster.Items.Add(newMenuItem1);
You can add This
var someMenu= new MenuItem("Foo");
someMenu.MenuItems.Add( new MenuItem("foo "));
That format doesn't work for me. I'm forced to use:
// People often exclude the applicable "using" statements--so samples don't work!
using System.Web.UI.WebControls;
// Create the MAIN menu item
MenuItem mnuMenuItem = new MenuItem();
// Create the SUB menu item
MenuItem mnuSubMenuItem = new MenuItem();
// Create the SUB menu item, "under" the MAIN menu item!
mnuMenuItem.ChildItems.Add(mnuSubMenuItem);

Adding items to PopupMenu

I am trying to find how I can add items to devExpress PopupMenu. I have tried the following:
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
listBoxMenu.ItemLinks.Add(manager.Items["Remove item"]);
listBoxMenu.ItemLinks.Add(manager.Items["Clear items"]);
As shown here http://documentation.devexpress.com/#WindowsForms/CustomDocument5472 (at the bottom), but it gives me an error saying the item is not initialized.
What is the proper way to add items? I can't find it anywhere.
Edit, here is how I did it:
//Creates the popup menu to be used for the keywords listbox
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
item = new BarButtonItem(manager, "Copy");
item2 = new BarButtonItem(manager, "Clear Item");
item3 = new BarButtonItem(manager, "Clear All Items");
listBoxMenu.ItemLinks.Add(item);
listBoxMenu.ItemLinks.Add(item2);
listBoxMenu.ItemLinks.Add(item3);
//Adds the seperator on the second item
item2.Links[0].BeginGroup = true;
manager.ItemClick += manager_ItemClick;
Check this code snippet and implement using the same way.
//create popup and manage objects
private DevExpress.XtraBars.BarManager barManager1;
private DevExpress.XtraBars.PopupMenu buttonContextMenu;
DevExpress.XtraBars.BarButtonItem menuButtonExport = new DevExpress.XtraBars.BarButtonItem();
DevExpress.XtraBars.BarButtonItem menuButtonSave = new DevExpress.XtraBars.BarButtonItem();
public TestForm8()
{
InitializeComponent();
barManager1 = new BarManager();
this.barManager1.Form = this;
buttonContextMenu = new DevExpress.XtraBars.PopupMenu(barManager1);
this.buttonContextMenu.Name = "subViewContextMenu";
menuButtonExport.Caption = "E&xport";
menuButtonExport.Id = 1;
menuButtonExport.Name = "menuButtonExport";
menuButtonExport.ItemClick += new ItemClickEventHandler(menuButtonExport_ItemClick);
menuButtonSave.Caption = "S&ave";
menuButtonSave.Id = 2;
menuButtonSave.Name = "menuButtonSave";
menuButtonSave.ItemClick += new ItemClickEventHandler(menuButtonSave_ItemClick);
//add items to barmanager
this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
menuButtonExport,
menuButtonSave
});
//create links between bar items and popup
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonExport"]);
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonSave"]);
//finally set the context menu to the control or use the showpopup method on right click of control
barManager1.SetPopupContextMenu(btnInsert, buttonContextMenu);
}
Ref by step to include popup:
How to: Create a popup menu
How to: Add items to a container bar item (menu)
Populating Popup Menus
BarManager.SetPopupContextMenu Method
Your manager is empty:
manager = new BarManager();
The example you linked to is using a BarManager that was already created: barManager1, which I assume was created in the designer and populated with items.
From their BarManager help page:
After a BarManager has been added to a form/user control, you can create bars and bar commands using context menus right on the form, using the bar manager's Customization Window or its Designer. Please see the Toolbars Customization section, to learn more.

System.Windows ContextMenu ItemClick Event?

I'm making a WPF application, but in my code I need to make a ContextMenu, it seemed really easy:
_menu = new ContextMenu();
_menu.Items.Add("My menu item");
Then I used it, and everything works like a charm.
However, I need to know when "My menu item" is clicked, but I can't seem to find the right event, I'm searching for something like ItemClick event, but cant't find it...
Try adding an item that is clickable rather than just a string. For example:
_menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += MyClickHandler;
item.Header = "My Menu Item";
_menu.Items.Add(item);
I never did it in code, always used XAML.
However, it is something like this:
_menu = new ContextMenu();
MenuItem mi = new MenuItem();
mi.Items.Add("My menu item");
mi.Click += (sender,args) =>
{
// Do what you want, or instead of a lambda
// you can even add a separate method to the class
};
_menu.Items.Add(mi);
The only doubt is adding the text to the menu item. You'll have to try as in the example or maybe add a TextBlock to the MenuItem.Items collection
I think you want something like this:
_menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "My menu item";
item.Click += new RoutedEventHandler(item_Click);
_menu.Items.Add(item);

Categories