ContextMenuBehavior allows you to easily display a menu on a tap or right click.

In your view you should bind an element which will have a context menu and a list of your commands:
<TextBlock behaviors:ContextMenuBehavior.Commands="{Binding ContextMenuCommands}"
           Text="{Binding Text}"
           MinWidth="300"
           Height="40"/>


In your model you should define the list of commands:
public class MyViewModel : ViewModel
{
        private IList<UICommand> _contextMenuCommands;
        private string _text;

        public string Text
        {
            get { return _text; }
            set { OnPropertyChange(ref _text, value); }
        }

        public IList<UICommand> ContextMenuCommands
        {
            get
            {
                return _contextMenuCommands ?? (_contextMenuCommands = new List<UICommand>
                {
                    new UICommand("Copy", OnCopy),
                    new UICommand("Paste", OnPaste),
                });
            }
        }

        private void OnCopy(IUICommand command)
        {
            var content = new DataPackage();
            content.SetText(Text);
            Clipboard.SetContent(content);
        }

        private async void OnPaste(IUICommand command)
        {
            var content = Clipboard.GetContent();
            Text = await content.GetTextAsync();
        }
}

Last edited Jan 24, 2013 at 4:27 AM by mglukhankov, version 2

Comments

No comments yet.