VIBlend
Home Products Downloads Purchase Support Forum About Blog

Silverlight Ribbon Key Tips

by viblend 6. September 2011 06:07

VIBlend RibbonBar offers an easy way to execute commands using Key Tips.  When an end-user presses the Space or F10 key, Key Tips automatically appear. They disappear when the command associated to a KeyTip is executed, or when the user presses the Esc key or clicks a mouse button.

The example code below demonstrates how to create a KeyTip for a UIElement: 

<viblendRibbonBar:RibbonBar.QuickAccessItems>
      <editors:Button Command="{Binding SaveCommand}" viblendRibbonBar:QuickAccessToolbar.MenuItemText="Save" viblendRibbonBar:KeyTipService.Text="1" viblendRibbonBar:KeyTipService.Command="{Binding SaveCommand}" viblendRibbonBar:KeyTipService.Key="D1"/>
      <editors:Button Command="{Binding RedoCommand}" viblendRibbonBar:QuickAccessToolbar.MenuItemText="Redo" viblendRibbonBar:KeyTipService.Text="2" viblendRibbonBar:KeyTipService.Command="{Binding RedoCommand}" viblendRibbonBar:KeyTipService.Key="D2"/>
      <editors:Button Command="{Binding UndoCommand}" viblendRibbonBar:QuickAccessToolbar.MenuItemText="Undo" viblendRibbonBar:KeyTipService.Text="3" viblendRibbonBar:KeyTipService.Command="{Binding UndoCommand}" viblendRibbonBar:KeyTipService.Key="D3"/>
</viblendRibbonBar:RibbonBar.QuickAccessItems>

The KeyTip.Text property allows you to set the text which will be displayed in the KeyTip. The Command and CommandParameter properties allow you to associate a Command to the KeyTip. The command is executed when the user presses the Key associated to the KeyTip.

The sample code below demonstrates how to create the commands from the above code.

C#

public RibbonCommand SaveCommand { get; set; }
public RibbonCommand UndoCommand { get; set; }
public RibbonCommand RedoCommand { get; set; }

public KeyTips()
{          
    InitializeComponent();

    this.SaveCommand = new RibbonCommand(Save, CanExecuteCommand);
    this.UndoCommand = new RibbonCommand(Undo, CanExecuteCommand);
    this.RedoCommand = new RibbonCommand(Redo, CanExecuteCommand);
    this.ribbonBar1.DataContext = this;
 }

private void Save(object parameter)
{
    MessageBox.Show("Save Command");
}

private void Undo(object parameter)
{
    MessageBox.Show("Undo Command");
}

private void Redo(object parameter)
{
    MessageBox.Show("Redo Command");
}

private bool CanExecuteCommand(object parameter)
{
     return true;
}

VB.NET

Private privateSaveCommand As RibbonCommand
Public Property SaveCommand() As RibbonCommand
    Get
        Return privateSaveCommand
    End Get
    Set(ByVal value As RibbonCommand)
        privateSaveCommand = value
    End Set
End Property
Private privateUndoCommand As RibbonCommand
Public Property UndoCommand() As RibbonCommand
    Get
        Return privateUndoCommand
    End Get
    Set(ByVal value As RibbonCommand)
        privateUndoCommand = value
    End Set
End Property
Private privateRedoCommand As RibbonCommand
Public Property RedoCommand() As RibbonCommand
    Get
        Return privateRedoCommand
    End Get
    Set(ByVal value As RibbonCommand)
        privateRedoCommand = value
    End Set
End Property

Public Sub New()
    InitializeComponent()

    Me.SaveCommand = New RibbonCommand(AddressOf Save, AddressOf CanExecuteCommand)
    Me.UndoCommand = New RibbonCommand(AddressOf Undo, AddressOf CanExecuteCommand)
    Me.RedoCommand = New RibbonCommand(AddressOf Redo, AddressOf CanExecuteCommand)
    Me.ribbonBar1.DataContext = Me
End Sub

Private Sub Save(ByVal parameter As Object)
    MessageBox.Show("Save Command")
End Sub

Private Sub Undo(ByVal parameter As Object)
    MessageBox.Show("Undo Command")
End Sub

Private Sub Redo(ByVal parameter As Object)
    MessageBox.Show("Redo Command")
End Sub

Private Function CanExecuteCommand(ByVal parameter As Object) As Boolean
     Return True
End Function


You can change the KeyTips activation and deactivation keys by using the RibbonBar's KeyTipsActivationKey and KeyTipsDeActivationKey properties. 

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET