Reputation: 183
Hi i need to set content menu in windows phone app.I tried some code but content menu does not fire, So any one tell me why my code is not working.what is wrong in my code.
My code is given below:
<Button Width="113" Click="Home" BorderThickness="0" HorizontalAlignment="Left" Height="87" >
<Image Source="Images/home_30.png" Stretch="Uniform" VerticalAlignment="Top" Height="66" Width="68" />
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="GestureListener_Tap" />
</toolkit:GestureService.GestureListener>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="menu">
<toolkit:MenuItem Header="Add"/>
<toolkit:MenuItem Header="Update"/>
<toolkit:MenuItem Header="Delete"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>
My C# code
private void GestureListener_Tap(object sender, GestureEventArgs e)
{
Button button = sender as Button;
ContextMenu contextMenu = ContextMenuService.GetContextMenu(button);
if (contextMenu.Parent == null)
{
contextMenu.IsOpen = true;
}
}
I am using this code but menu button does not fire.
Upvotes: 2
Views: 2317
Reputation: 1386
What Rithesh Baradi posted is correct when you have one object and you put a context menu for it, but might not be so handy when the context menu applies to a list. Consider the following solution from Windows Phone Toolkit (for simplicity I only took one command):
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Header1" Command="{Binding Header1Command}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
The code behind in your ViewModel:
public partial class MainPage : PhoneApplicationPage
{
ViewModel _viewModel;
public ContextMenuSample()
{
InitializeComponent();
_viewModel = new ViewModel();
_viewModel.Notify += OnViewModelNotify;
LayoutRoot.DataContext = _viewModel;
}
void OnViewModelNotify(object sender, CommandEventArgs e)
{
Debug.WriteLine(string.Format("ICommand: {0}", e.Message));
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine((string)((MenuItem)sender).Header);
}
}
public class CommandEventArgs : EventArgs
{
public CommandEventArgs(string message)
{
Message = message;
}
public string Message { get; private set; }
}
public class ViewModel
{
public ICommand Header1Command { get; private set; }
public event EventHandler<CommandEventArgs> Notify;
public ViewModel()
{
Header1Command= new Header1ICommand();
((Header1ICommand)Header1Command).Notify += OnNotify;
}
private void OnNotify(object sender, CommandEventArgs e)
{
var notify = Notify;
if (notify != null)
{
notify(this, e);
}
}
}
public class Header1ICommand: ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
var unused = CanExecuteChanged;
var notify = Notify;
if (notify != null)
{
notify(this, new CommandEventArgs("Header1ICommand- " + (parameter ?? "[null]")));
}
}
public event EventHandler<CommandEventArgs> Notify;
}
An alternative solution to ICommand MVVM Light Toolkit - RelayCommand. There are plenty of examples but it pretty much works the same way with ICommands - its just more simple since you have to write less code! Choose what suits you best.
Final Note: Be careful with ContextMenu since I had problems before where the DataContext
of the ContextMenu
wouldn't change while it did for the rest of the elements (textblocks etc).
Upvotes: 0
Reputation: 550
you have a Click event for the MenuItem
please try something like this
<toolkit:MenuItem x:Name="Copy" Header="Copy" Click="Copy_Click"/>
on you code behind
private void Copy_Click(object sender, RoutedEventArgs e)
{
//handle the event here
}
Upvotes: 1