WPF Menu use (rpm)

Menu use

1.1 MenuData.xml Bunken

<?xml version="1.0" encoding="utf-8" ?>
<Data xmlns="">
<Operation Name="部门管理" Gesture="F" Ico="/Images/out.ico">
<Operation Name="部门列表" Gesture="Control+P" Uid="/Department/DpmtList.xaml" Model="Jump"/>
<Operation Name="新增部门" Gesture="Control+W" Uid="/Department/AddDpmt.xaml" Model="Show"/>
</Operation>
<Operation Name="员工管理" Gesture="E" Ico="/Images/admin.ico">
<Operation Name="员工列表" Gesture="Control+C" Uid="/Employee/EmpList.xaml" Model="Jump"/>
<Operation Name="新增员工" Gesture="Control+X" Uid="/Employee/AddEmp.xaml" Model="Show"/>
</Operation>
</Data>
1.2 前台代码

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="菜单" Height="500" Width="800" WindowStartupLocation="CenterScreen">
<Window.Resources>
<!--数据源-->
<XmlDataProvider x:Key="dsMenu" Source="MenuData.xml" XPath="Data/Operation" />
<!--菜单模板-->
<HierarchicalDataTemplate DataType="Operation" ItemsSource="{Binding XPath=Operation}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding XPath=@Ico}" Height="24" Width="24" Margin="10,0" />
<TextBlock Text="{Binding XPath=@Name}" Margin="10,0"/>
<TextBlock Text="{Binding XPath=@Gesture}" />
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="24"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--菜单-->
<StackPanel Grid.Row="0" MenuItem.Click="StackPanel_Click_1">
<Menu ItemsSource="{Binding Source={StaticResource dsMenu}}"/>
</StackPanel>
<!--显示内容-->
<Frame Grid.Row="1" x:Name="mainFrame"= The Source "/ Index.xaml" = NavigationUIVisibility to "Hidden" /> 1.3 Background Code</ the Window>
</ the Grid>

/// <summary>
/// 菜单点击事件
/// </summary>
private void StackPanel_Click_1(object sender, RoutedEventArgs e)
{
MenuItem mi = e.OriginalSource as MenuItem;
XmlElement xe = mi.Header as XmlElement;
string name = xe.Attributes["Name"].Value;
string uid = xe.Attributes["Uid"].Value;
string model = xe.Attributes["Model"].Value;
//Jump:使用Frame跳转页面;Show:使用NavigationWindow弹出页面
if (model == "Jump")
{
JumpPage(uid);
}
else if (model == "Show")
{
ShowPage(Name, uid);
}
}

/// <summary>
/// 使用Frame跳转页面
/// </summary>
private void JumpPage(string uid)
{
if (!String.IsNullOrWhiteSpace(uid))
{
this.mainFrame.Navigate(new Uri(uid, UriKind.Relative));
}
}

/// <summary>
/// 使用NavigationWindow弹出页面
/// </summary>
private void ShowPage(string title,string uri)
{
NavigationWindow window = new NavigationWindow();
window.Title = title;
window.Width = 300;
window.Height = 200;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.ResizeMode = ResizeMode.NoResize;
window.Source = new Uri(uri, UriKind.Relative);
window.ShowsNavigationUI = false;
window.Show();
}
2、TreeView的使用

2.1 Code Reception

<Window x:Class="WpfApplication1.Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="菜单" Height="500" Width="800" WindowStartupLocation="CenterScreen">
<Window.Resources>
<!--菜单样式-->
<Style TargetType="{x:Type TreeView}">
<Setter Property="Height" Value="550"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Background" Value="#059ad7"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="5,10,0,0"/>
</Style>
<!--菜单项样式-->
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="FontSize" Value="16"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="190"/>
<ColumnDefinition Width="3"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--菜单-->
<StackPanel Grid.Column="0" Background="#059ad7">
<TreeView x:Name="tvMenu" SelectedItemChanged="TreeView_SelectedItemChanged">
<TreeViewItem Header = "menu" IsExpanded = "True"> <TreeViewItem Header = "Department list" Uid = "/ Department / DpmtList.xaml
<TreeViewItem Header = "Sector Management" IsExpanded = "True">

<TreeViewItem Header="新增部门" Uid="/Department/AddDpmt.xaml"/>
</TreeViewItem >
<TreeViewItem Header="员工管理" IsExpanded="True">
<TreeViewItem Header="员工列表" Uid="/Employee/EmpList.xaml"/>
<TreeViewItem Header="新增员工" Uid="/Employee/AddEmp.xaml"/>
</TreeViewItem >
</TreeViewItem>
</TreeView>
</StackPanel>
<!--隔栏-->
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Background="#c3c1c1" ShowsPreview="True" Width="3" />
<!--中间内容-->
<Frame Grid.Column="3" x:Name="mainFrame" Source="/Index.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
2.2 后台代码

/// <summary>
/// 菜单跳转事件
/// </summary>
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeView tvMenu = sender as TreeView;
TreeViewItem tvItem = tvMenu.SelectedItem as TreeViewItem;
string uid = tvItem.Uid;
if (!String.IsNullOrWhiteSpace(uid))
{
this.mainFrame.Navigate(new Uri(uid, UriKind.Relative));
}
}
3、其他内容

1、Menu

Menu items are placed horizontally it is, by default, the gray bar in the background.
Menu to add to its unique public API is IsMainMenu ItemsControl base class attributes. When true (the default), the user presses the Alt or F10 key, the menu focus.
MenuItem is spearheaded Items Controls (inherited from HeaderedItemControl), its head is actually a main object. If Items are then sub-elements will be used as
display the submenu. MenuItem using the underscore prefix to support access keys (access key).
Separator (dividing line) is a simple control.
Icon-- allowed to add any type of object next to Header. Icon object will be rendered the same as Header, although it is usually a small picture.
IsCheckable-- let behavior like a MenuItem CheckBox control.
InputGestureText-- with gestures (gesture) to identify a relevant one item (the most common is the keyboard shortcuts, such as Ctrl + O).
5 events: Checked, Unchecked, SubmenuOpened, SubmenuClosed and Click.
To MenuItem to set a keyboard shortcut, you should use its Command property.
2, ContextMenu (context menu)

It is a simple container preservation of MenuItem and Separator. ContextMenu but not directly embedded into an element tree, the property must be a proper load it into the control.
IsOpen property and Opened / Closed event.
By default, the mouse pointer is located at the upper left corner of the menu, but it can be changed to a value other MousePoint Placement (e.g. Absolute), or set its attributes VerticalOffset HorizontalOffset and to adjust this behavior.
ContextMenuService static class, which contains a number of additional attributes, respectively corresponding to those properties directly defined by the ContextMenu.
3, Other Items controls

A、TreeView

Expand and collapse with the node to display hierarchical data.
In the TreeView must explicitly with Item TreeViewItem packaging, filled with TreeViewItem, as MenuItem TreeViewItem like, having a control head, which contains Header property of the current item, and its set of saved Items the child (children also TreeViewItem).
TreeViewItem have two attributes IsExpanded IsSelected and convenient. There are four events that correspond to these two properties of four states: Expanded, Collapsed, Selected and Unselected.
Also supports rich keyboard navigation, plus and minus keys to expand or collapse a key, arrow keys, Page Up, Page Down, Home and End key focus from one item to another item movement.
B, ToolBar

For many small buttons (or other controls) are grouped.
ToolBar can be placed anywhere in the element tree, but often put them in a called ToolBarTray of FrameworkElement.
Users can drag or redefine ToolBar ToolBar ,. Unless ToolBarTray of IsLocked property is set to true.
A ToolBarTray Orientation property, it can be set so that all Vertical ToolBar items arranged vertically.
The default is the last element of the first to be moved to the overflow area, but you can OverflowMode additional properties to control the behavior of each item of overflow. With this property, you can put an item marked as AsNeeded (default, as required overflow), Always or Never.
System.Windows.Input KeyboardNavigat ion class name space defines some additional properties used to customize the keyboard behavior.
ToolBar is actually with a head Item controls (like MenuItem and TreeViewItem). It's Header property is never displayed, but it can be used to implement other features of ToolBarTray.
C, StatusBar

StatusBar behaves like Menu, it's just put it horizontally arranged entries, usually at the bottom of the window to display status information.
StatusBar control provides a template for the Separator, Separator as vertical lines are rendered.
The StatusBar item (except Separator) is implicitly wrapped in a StatusBarItem in, but you can still explicitly packaged.
---------------------
Author: pan_junbiao
Source: CSDN
Original: https: //blog.csdn.net/pan_junbiao/article/details/50987434
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/LiZhongZhongY/p/11075375.html