How to create vertical menu control in WPF - wpf

Iam trying to create vertical menu control but dont know from where to start. I am basically coming from winform to wpf and using telerik controls and this control which am using is called RadMenuControl.
Please view the video in link and suggest me accordingly with code and helping links from where i can start, thanks in advance.
MenuItem Video

You can change the ItemsPanel of Menu control.
Inheritance heirarchy :
ItemsControl > MenuBase > Menu.
<Menu Height="Auto" Width="Auto" FontSize="18">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Styling">
<MenuItem Header="Background"/>
</MenuItem>
<MenuItem Header="Measurement">
<MenuItem Header="Width"/>
<MenuItem Header="Height"/>
</MenuItem>
<MenuItem Header="Font"/>
</Menu>

If you need an similar functionality, you may consider using a StackPanel and keep stacking button items to them.
<StackPanel>
<Button Content="Home"/>
<Button Content="Edit"/>
<Button Content="View"/>
</StackPanel>

If you are using Telerik controls would you try RadPanelBar or RadOutlookBar. Surely your requirement will be satisfied one of these two controls.
RadPanelBar

Related

Creating a dropdown toolbar in xaml

I've recently started working with XAML in WPF and I'm working on someone elses code. I'm trying to rework a drop down toolbar/menu, because of some display issues but the drop down is created as a <Popup>. I feel technically a menu is not really a popup...
So my question is, is it appropriate to ever create sub/drop down menus using the popup tag? Or would it be better suited for <stackPanel> or something else?
Thanks
As someone already stated, you could use a ComboBox and make it look like a drop down menu. You could use a Popup to make it look like drop down menu either. Both require quite some template work but I wouldn't have anything against it as long as it looks as you want it to look.
For a classic menu & drop down I would just use the Menu control. Here some information about it. Depending on your situation, it's probably just an easier way to get the same result
Code from site:
<Window x:Class="WpfTutorialSamples.Common_interface_controls.MenuSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuSample" Height="200" Width="200">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" />
<MenuItem Header="_Open" />
<MenuItem Header="_Save" />
<Separator />
<MenuItem Header="_Exit" />
</MenuItem>
</Menu>
<TextBox AcceptsReturn="True" />
</DockPanel>

Commands causing slow down

First off, I'm fully aware of how contrived this example is, and it's lack of virtualization, but I'm using it to demonstrate a problem I've been experiencing in a more complicated system that would be very difficult to show. And this seems like a simple way to demonstrate it.
The following code creates a menu and an ItemsControl bound to a large list of strings(I'm using 10,000), each being viewed as a button.
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="Test">
<MenuItem Header="A" />
<MenuItem Header="B" />
<MenuItem Header="C" />
</MenuItem>
</Menu>
<Expander IsExpanded="True">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding LotsOfStrings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="Open" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Expander>
</DockPanel>
As expected, this is slow to load, but once it has loaded, the 'Test' menu is responsive (that is, there is no noticeable lag as you mouse over the menu items).
Now make the following small change of adding a command to the button.
<Button Content="Open" Command="Open"/>
Now the Menu becomes horribly laggy. For some reason, mousing over different menu items calls CommandManager.InvalidateRequerySuggested() which gets executed for each button, causing the slow down.
So I'm wondering if there is anyway of disabling the Commanding updates on controls that aren't visible? so in this example, if the expander is collapsed, can I some how stop the buttons from still handling the commanding updates?

Xceed splitbutton with menu items and sub menuitems

I`m trying to have a dropdown menu under a split button. Some of these menuItems should have sub-menu items. If you want an example, click on the bookmark button in Firefox (top right).
I can't use Menu, because that is always oriented horizontally. So I went with a stackpanel:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xctk:SplitButton Content="SplitButton" BorderThickness="1" BorderBrush="Black" Margin="0,0,408,290">
<xctk:SplitButton.DropDownContent>
<StackPanel Width="161" HorizontalAlignment="Left">
<MenuItem Header="MenuItem1" HorizontalAlignment="Left" Width="517">
<MenuItem.Items>
<MenuItem Header="submenuItem1"/>
<MenuItem Header="submenuItem2"/>
</MenuItem.Items>
</MenuItem>
<MenuItem Header="MenuItem2"/>
<MenuItem Header="MenuItem3"/>
</StackPanel>
</xctk:SplitButton.DropDownContent>
</xctk:SplitButton>
</Grid>
Problem here is that the sub menu items don't show up. They don't even have the little arrows next to them. You can do this without the SplitButton, just leaving the stackpanel and everything in it, you'll have the same problem. I've tried putting the parent Menu item in its own tag, but I want the sub-menu items to appear to the right of their parent (just like the firefox example: Assuming your firefox window is not maximized, and you've allowed enough screen-space for the item to appear).
the way you're using the MenuItem control is wrong, it is meant to be hosted inside the Menu control or another MenuItem.
As for the solution to your problem, there are two. the first one is to write a custom control that reuses the ContextMenu to host the menu items, you will write something like this:
<m:SplitButton Content="Split Button" Placement="Bottom">
<MenuItem Header="MenuItem 1"/>
<MenuItem Header="MenuItem 2">
<MenuItem Header="MenuItem 1"/>
<MenuItem Header="MenuItem 2"/>
</MenuItem>
</m:SplitButton
http://www.codeproject.com/Articles/20612/A-WPF-SplitButton
the second approach is to host the Menu control inside the DropDownContent and re-style everything, there will be a lot of xaml markup.
the Menu control will be easy to re-style, the only thing you need to do is make sure that menu items are displayed vertically instead of horizontally, using the following markup:
<Style TargetType="Menu">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" IsItemsHost="True" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
the hardest part is styling the MenuItems, they are styled based on their Role property. It can have four possible values:
TopLevelHeader : direct child of Menu with sub-menu items.
TopLevelItem : direct child of Menu without sub-menu items.
SubmenuHeader : direct child of MenuItem with sub-menu items.
SubmenuItem : direct child of MenuItem without sub-menu items.
Regards

VS2010 WPF - Can I create a menu with menu items just via the UI (not editing XAML?)

Just trying to drag my first Menu control onto a WPF application in VS2010.
Is there a way to (via the VS2010 UI) setup the menu items etc? Or does one have to jump into the XAML to do this?
Also it seems like the Menu control, after I drag it onto the window, exists at the top of the Window. However I was expecting it to be rendered as a typical Windows menu where it's right at the top associated with the window itself (not the window contents), if that makes sense. Does the VS2010 "menu" item from the toolbox give you the "traditional" windows application menu?
I would really encourage you to read up on Panels (and Attached Properties) before you start playing with the controls to understand how they are laid out (Especially the difference between Panels and ContentControls is key). In WPF panels decide how the controls are laid out (at least the basics within which controls get a wee say). It sounds much like you are trying to do WPF the WinForms way - and you will end up really frustrated and needing lots of tranquillizers before the hour turns nigh... :)
In the VS Studio the template uses a Grid as the basis for layouting - which by default centers and stretches content (as well as overlaying controls), so just dragging a menu in there will provide insensible designs.
As for jumping into XAML - I never use the ToolBox and the Visual Designer. It's a matter of taste of course, but if you're used to using VS (in contrast to Blend), I find it easier to understand what is happening when I edit the raw XAML.
A few starter resources: link and link. And for a simpler learning environment for getting started - I enjoyed Kaxaml a lot (which is an editor build in XAML/WPF albeit in .Net 3.5 sp1).
EDIT: A small sample - just copy everything between the Window-tags and paste it between the ones in your template that Visual Studio gives you:
<Window ....>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open"/>
<MenuItem Header="_Save"/>
<MenuItem Header="_Exit"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="C_ut"/>
<MenuItem Header="_Copy"/>
<MenuItem Header="Paste"/>
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="About"/>
</MenuItem>
</Menu>
<GroupBox Header="Some interesting controls go here">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="_First property"/>
<TextBox Grid.Column="1"/>
<Label Grid.Row="1" Content="_Second property"/>
<TextBox Grid.Column="1" Grid.Row="1"/>
</Grid>
</GroupBox>
</DockPanel>
</Window>

Right Align MenuItems in WPF

Can i right align the menu items in WPF?
Thanks
Sharath
<Menu HorizontalAlignment="Stretch" FlowDirection="RightToLeft">
<MenuItem HorizontalAlignment="Right" Header="aaa">
</MenuItem>
<MenuItem HorizontalAlignment="Right" Header="bbb">
</MenuItem>
</Menu>
Yes, you can.
Although the implementation is a little screwy.
If you want to have the menu items in the top menu go from right to left, add FlowDirection="RightToLeft" to your Menu.
If you want to have an item aligned right in a dropdown, do the following:
<MenuItem>
<MenuItem.Header>
<TextBlock HorizontalAlignment="Right" >Content</TextBlock>
</MenuItem.Header>
</MenuItem>
If you want to do both, you actually have to set HorizontalAlignment="Left" instead of right, as the FlowDirection reverses the right and left in the Alignments. I don't know why, but that's what you have to do.
How do I right-align the 'help' menu item in WPF?
I like the second answer with the grid way.

Resources