Drop-Down Menu Button with Telerik ContextMenu for Silverlight

Posted by: the telerik blogs, on 07 Sep 2009 | View original | Bookmarked: 0 time(s)

Sometimes, when a button is clicked we want to display a drop-down menu with options. This is handy interface, usually present in toolbars, but sometimes - as standalone buttons, like the Save As button in the the VS2008 Save As dialog. Sometimes the drop-down buttons consist of two parts action and menu, and are called split buttons. In this post I will demonstrate how to achieve the drop-down menu button part. If you need a split button, just add another button and keep track of what was the last clicked menu item. Note, that with Q3 2009 we will add similar button to RadControls for Silverlight / WPF, that will display a popup when it is clicked and you will be able to put whatever you like inside that popup. In this case, however, I want to display a context menu upon the button click.

Initially, I created an attached behavior, which handled the button and context menu state changes, etc. It worked, but was complex and then I figured that the problem can be solved with a simple binding. Here is the final piece of code:

<ToggleButton Content="Click me" HorizontalAlignment="Left" 
 IsChecked="{Binding IsOpen, ElementName=context, Mode=TwoWay}"> <telerikNavigation:RadContextMenu.ContextMenu> <telerikNavigation:RadContextMenu x:Name="context" Placement="Bottom" ItemClick="RadContextMenu_ItemClick"> <telerikNavigation:RadMenuItem Header="Item 1" /> <telerikNavigation:RadMenuItem Header="Item 2" /> <telerikNavigation:RadMenuItem Header="Item 3" /> </telerikNavigation:RadContextMenu> </telerikNavigation:RadContextMenu.ContextMenu> </ToggleButton>

 

There is one important thing that deserves to be noted - by default, RadContextMenu is displayed next to the mouse cursor. To display the menu below the button I had to set the Placement property of RadContextMenu to Bottom. You could use Top, Left or Right, depending on your preferences. RadContextMenu will automatically detect the window boundaries and will adjust its position if there isnt enough space at the desired placement.

The final result is this:

contextmenubutton

You can download the sample application from here:

MenuButton

Thank you for reading, I hope this helps.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: C# | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 4254 | Hits: 18

Similar Posts

  • Telerik Introduces Free Web Testing Framework for ASP.NET AJAX and Silverlight more
  • Generic WPF Drag and Drop Adorner more
  • A guide to Silverlight 3 new features more
  • "Silverlight: Write and Win!" Contest more
  • Google Chrome works for SharePoint users, less so for administrators more
  • Optimizing the load size of Telerik Silverlight controls more
  • Silverlight 2 Beta2 Released more
  • Creating Controls in Code more
  • Tip of the Day: Content - It Ain't Just Text more
  • Searching for the "Holy Grail" of Menu Item Customization more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD