新建wpf项目
直接F5运行即可
常用控件
菜单
简单菜单
- 添加图标
- 添加图片文件夹
- 添加一张图片
- xaml布局文件
<Window x:Class="wpf_hello.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf_hello"
mc:Ignorable="d"
Title="wpf_hello" Height="450" Width="800">
<!-- 自定义命令 -->
<Window.Resources>
<RoutedUICommand x:Key="cmd_open_file" Text="open file" />
</Window.Resources>
<!-- 自定义命令 绑定快捷键 -->
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+O" Command="{StaticResource cmd_open_file}" />
</Window.InputBindings>
<!-- 自定义命令 相应函数 -->
<Window.CommandBindings>
<CommandBinding Command="{StaticResource cmd_open_file}"
CanExecute="cmd_open_file_CanExecute"
Executed="cmd_open_file__Executed" />
</Window.CommandBindings>
<Grid>
<!-- Auto 表示高度自适应, * 表示高度占用剩下的空间 -->
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<MenuItem Header="文件(_F)">
<MenuItem Header="新建" />
<!--
Command 指定自定义命令
InputGestureText 内容就是显示的快捷键
-->
<MenuItem Header="打开文件..." Command="{StaticResource cmd_open_file}" InputGestureText="Ctrl+O">
<MenuItem.Icon>
<StackPanel>
<Image Source="/image/icon_open_file.png" />
</StackPanel>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
- cs文件
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 wpf_hello
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// cmd_open_file 打开文件 是否能执行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmd_open_file_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
/// <summary>
/// cmd_open_file 打开文件 handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmd_open_file__Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("打开文件", "打开文件", MessageBoxButton.YesNo);
}
}
}
可以选中的菜单
- 添加一个菜单
<!-- 可选择的菜单 -->
<MenuItem Name="menuitem_auto_transform_format" Header="自动转换格式" IsCheckable="True"
Checked="menuitem_auto_transform_format_checked"
Unchecked="menuitem_auto_transform_format_unchecked"/>
- 选中与取消选中事件
/// <summary>
/// 菜单 文件->自动转换格式 被选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuitem_auto_transform_format_checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("自动转关格式被选中", "可选择菜单", MessageBoxButton.YesNo);
}
/// <summary>
/// 菜单 文件->自动转换格式 取消选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuitem_auto_transform_format_unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("自动选中格式被取消", "可选择菜单", MessageBoxButton.YesNo);
}
动态添加菜单
- 添加一个菜单并设置
Name
属性
<MenuItem Name="menuitem_recent_file" Header="最近打开的文件">
<Separator />
<MenuItem Header="Clear History" />
</MenuItem>
- 添加一个按钮
<Button Width="Auto" Click="add_recent_file">添加一个最近打开文件</Button>
- 按钮的事件处理函数
private static int recent_file_count = 1;
/// <summary>
/// 添加一个最近打开的文件,同时动态添加一个菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void add_recent_file(object sender, RoutedEventArgs e)
{
// 查找菜单
MenuItem miRecentFile = this.FindName("menuitem_recent_file") as MenuItem;
ItemCollection items = miRecentFile.Items;
MenuItem menuItem = new MenuItem
{
Header = "新添加的文件 " + recent_file_count++
};
// 向后追加
// items.Add(menuItem);
// 在最前面追加
items.Insert(0, menuItem);
}
右键菜单
直接在控件中声明
<ContentControl Grid.Row="2">
<TextBlock>
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="执行" />
<MenuItem Header="sql 内容助理" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</ContentControl>
在 Resource 中声明
可以多个控件引用
- 声明
<Window.Resources>
<!-- 声明右键菜单 -->
<ContextMenu x:Key="text_block_context_menu">
<MenuItem Header="执行" />
<MenuItem Header="sql 内容助理" />
</ContextMenu>
</Window.Resources>
- 引用
<TextBlock ContextMenu="{StaticResource text_block_context_menu}"></TextBlock>