问题
I want to create a horizontal menu across the top of my user control whose items fill the whole space horizontally. There are six items and it's one level only - so it's not really a menu as such.
Can I do this with a menu? Or am I better off with using a six column grid with a button per col, or even a horizontal stack panel? Here is what I have so far:
<DockPanel>
<DockPanel DockPanel.Dock="Top" KeyboardNavigation.TabNavigation="None">
<Menu KeyboardNavigation.TabNavigation="Cycle" VerticalAlignment="Top" Background="DarkGray" Height="40">
<MenuItem Header="_New"/>
<MenuItem Header="_Load" />
<MenuItem Header="_Save" />
<MenuItem Header="_Validate" />
<MenuItem Header="_Import" />
<MenuItem Header="_Export"/>
</Menu>
</DockPanel>
</DockPanel>
回答1:
Menu derives from ItemsControl
so can just switch the ItemsPanel:
<DockPanel>
<DockPanel DockPanel.Dock="Top" KeyboardNavigation.TabNavigation="None">
<Menu KeyboardNavigation.TabNavigation="Cycle" VerticalAlignment="Top" Background="DarkGray" Height="40">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<MenuItem Header="_New"/>
<MenuItem Header="_Load" />
<MenuItem Header="_Save" />
<MenuItem Header="_Validate" />
<MenuItem Header="_Import" />
<MenuItem Header="_Export"/>
</Menu>
</DockPanel>
</DockPanel>
回答2:
<Menu KeyboardNavigation.TabNavigation="Cycle" VerticalAlignment="Top" Background="DarkGray" Height="40">
<MenuItem Header="File">
<MenuItem Header="_New"/>
<MenuItem Header="_Load" />
<MenuItem Header="_Save" />
<MenuItem Header="_Validate" />
<MenuItem Header="_Import" />
<MenuItem Header="_Export"/>
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste" />
</MenuItem>
</Menu>
Hope this will help. you need to add Menu subitems within the MenuItem. it itself is o0f List type.
来源:https://stackoverflow.com/questions/10189690/horizontal-menu