wpf hello world

新建wpf项目

在这里插入图片描述

直接F5运行即可

常用控件

菜单

简单菜单

  • 添加图标
  1. 添加图片文件夹
    在这里插入图片描述
  2. 添加一张图片
    在这里插入图片描述
  3. 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>
  1. 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);
        }
    }
}

可以选中的菜单

  1. 添加一个菜单
<!-- 可选择的菜单 -->
<MenuItem Name="menuitem_auto_transform_format" Header="自动转换格式" IsCheckable="True" 
          Checked="menuitem_auto_transform_format_checked"
          Unchecked="menuitem_auto_transform_format_unchecked"/>
  1. 选中与取消选中事件
/// <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);
}

动态添加菜单

  1. 添加一个菜单并设置Name属性
<MenuItem Name="menuitem_recent_file" Header="最近打开的文件">
    <Separator />
    <MenuItem Header="Clear History" />
</MenuItem>
  1. 添加一个按钮
<Button Width="Auto" Click="add_recent_file">添加一个最近打开文件</Button>
  1. 按钮的事件处理函数
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值