1. 界面效果如下:
2. 页面 .xaml代码如下:
<Window x:Class="MenuAndToolBar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DockPanel><!--停靠布局,让菜单栏和工具栏位于窗口顶部-->
<Menu DockPanel.Dock="Top">
<MenuItem Header="File"><!--菜单项为MenuItem,文字使用属性 Header-->
<MenuItem Name="menuOpen" Header="Open" Click="menuOpen_Click"></MenuItem>
<MenuItem Header="Save"></MenuItem>
<MenuItem Header="Delete"></MenuItem>
</MenuItem>
<MenuItem Header="Edit"></MenuItem>
<MenuItem Header="View"></MenuItem>
<MenuItem Header="About"></MenuItem>
</Menu>
<ToolBar DockPanel.Dock="Top"><!--ToolBar和StatusBar就是一个容器-->
<Button Content="Save"></Button>
<Button Content="SaveAll"></Button>
<Button Content="FileExplorer"></Button>
<Button Content="Start"></Button>
<Button Content="Test"></Button>
</ToolBar>
<TextBox DockPanel.Dock="Top" Text="This is my space" Height="243"></TextBox>
<StatusBar DockPanel.Dock="Bottom">
<TextBlock Text="statusBar"></TextBlock>
</StatusBar>
</DockPanel>
</Grid>
</Window>
3. 后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MenuAndToolBar
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void menuOpen_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Open the file you want.");
}
}
}