WPF的页面设计和实用功能实现

目录

一、TextBlock和TextBox

1. 在TextBlock中实时显示当前时间

二、ListView

1.ListView显示数据

三、ComboBox

1. ComboBox和CheckBox组合实现下拉框多选

四、Button

1. 设计Button按钮的边框为圆角,并对指针悬停时的颜色进行设置


一、TextBlock和TextBox

1. 在TextBlock中实时显示当前时间

可以通过绑定和定时器的方式来实现在TextBlock中显示当前实时时间。

<Window x:Class="RealTime.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:RealTime"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Name="timeTextBlock"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 FontSize="24"
                 Width="300"
                 Height="40"/>
    </Grid>
</Window>

cs

using System.Text;
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;
using System.Timers;
using System.Windows.Threading;

namespace RealTime
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer _timer;

        public MainWindow()
        {
            InitializeComponent();

            // 初始化定时器
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1); // 每秒更新时间
            _timer.Tick += Timer_Tick; // 定时器的 Tick 事件
            _timer.Start(); // 启动定时器
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            // 获取当前时间并更新 TextBox
            timeTextBlock.Text = DateTime.Now.ToString("yyyy/MM/dd:HH:mm:ss");
        }
    }
}

 生成效果

说明1:

  • DispatcherTimer:WPF 提供了 DispatcherTimer 类,它允许你在指定的时间间隔后执行代码,并且能够在 UI 线程上安全地更新 UI 元素。DispatcherTimer 每次触发时会调用 Tick 事件。

  • Interval:设置为每秒触发一次。

  • Tick 事件:每秒钟触发一次,在 Timer_Tick 方法中更新时间。这里使用了 DateTime.Now.ToString("HH:mm:ss") 格式来显示当前的小时、分钟和秒。

说明2:

  • DateTime.Now.ToString("HH:mm:ss") 显示小时、分钟和秒。

  • DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 显示完整的日期和时间。

二、ListView

1.ListView显示数据

<Window x:Class="ListView.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:ListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <ListView Name="StudentList"
                      MouseDoubleClick="StudentList_MouseDoubleClick"
                      Margin="10">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="姓名"
                                        Width="100"
                                        DisplayMemberBinding="{Binding Name}"></GridViewColumn>
                        <GridViewColumn Header="年龄"
                                        Width="100"
                                        DisplayMemberBinding="{Binding Age}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            <StackPanel Orientation="Horizontal">
                <Button Name="Mode1"
                        Margin="10"
                        HorizontalAlignment="Left"
                        Content="方式一"
                        Click="Mode1_Click"></Button>
                <Button Name="Mode2"
                        Margin="10"
                        HorizontalAlignment="Left"
                        Content="方式二"
                        Click="Mode2_Click"></Button>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

CS

using System.Collections.ObjectModel;
using System.Text;
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 ListView
{
    public class Student
    {
        public string? Name { get; set; }
        public string? Age { get; set; }
    }
    public partial class MainWindow : Window
    {
        public ObservableCollection<Student> Items { get; set; }
        public MainWindow()
        {
            InitializeComponent();           
        }

        private void Mode1_Click(object sender, RoutedEventArgs e)
        {
            StudentList.ItemsSource = null;
            StudentList.Items.Clear();
            // 初始化选项集合
            Items = new ObservableCollection<Student>
            {
                new Student { Name = "张三", Age = "20"},
                new Student { Name = "李四", Age = "21"},
                new Student { Name = "王五", Age = "22"},
                new Student { Name = "赵六", Age = "23"}
            };
            // 将Items集合绑定到ListView的ItemsSource
            StudentList.ItemsSource = Items;
        }

        private void Mode2_Click(object sender, RoutedEventArgs e)
        {
            StudentList.ItemsSource = null;
            StudentList.Items.Clear();
            StudentList.Items.Add(new Student { Name = "孙悟空", Age = "10000" });
            StudentList.Items.Add(new Student { Name = "悟能", Age = "5000" });
            StudentList.Items.Add(new Student { Name = "悟净", Age = "3000" });
            StudentList.Items.Add(new Student { Name = "唐僧", Age = "30" });
        }

        private void StudentList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(StudentList.SelectedItem is Student student)
            {
                MessageBox.Show("姓名:" + student.Name + ",年龄:" + student.Age);
            }
        }
    }
}

页面显示说明:

初始页面

通过 ItemsSource = 列表的形式,将数据绑定到页面上,点击方式一

通过Items.Add(new 自定义类 { 属性 = "", 属性 = ""....... })的方式绑定数据,点击方式二

双击查看选择了那一条数据

三、ComboBox

1. ComboBox和CheckBox组合实现下拉框多选

说明:实现ComboBox下拉框是CheckBox,通过CheckBox的勾选情况判断选择了哪些项目

<Window x:Class="ComboBox.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:ComboBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <!-- 定义多选ComboBox -->
            <ComboBox Name="multiSelectComboBox"
                      Width="200"
                      Height="30"
                      HorizontalAlignment="Left"
                      IsEditable="True"
                      StaysOpenOnEdit="True"
                      IsReadOnly="True"
                      Text="多选列表"
                      Margin="10">
                <!-- 定义ComboBox的ItemTemplate,包含一个CheckBox -->
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Name}"
                                  IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <!-- 按钮显示所选项目 -->
            <Button Content="查看选择了什么选项"
                    Width="170"
                    Height="30"
                    VerticalAlignment="Top"
                    HorizontalAlignment="Left"
                    Margin="10"
                    Click="ShowSelectedOptions_Click" />
        </StackPanel>
    </Grid>
</Window>

CS

using System.Collections.ObjectModel;
using System.Text;
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 ComboBox
{
    public class Student
    {
        public string? Name { get; set; }        
        public bool IsSelected { get; set; }
    }
    public partial class MainWindow : Window
    {
        public ObservableCollection<Student> Items { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            // 初始化选项集合
            Items = new ObservableCollection<Student>
            {
                new Student { Name = "张三"},
                new Student { Name = "李四"},
                new Student { Name = "王五"},
                new Student { Name = "赵六"}
            };
            // 将Items集合绑定到ComboBox的ItemsSource
            multiSelectComboBox.ItemsSource = Items;
        }

        // 显示已选择的选项
        private void ShowSelectedOptions_Click(object sender, RoutedEventArgs e)
        {    
            // 获取所有IsSelected为true的项目
            var selectedItems = Items.Where(item => item.IsSelected).Select(item => item.Name).ToList();
            // 显示选择的项目
            multiSelectComboBox.Text = "你选择了: " + string.Join(", ", selectedItems);
        }
    }
}

页面

点击下拉框,选择两个项目

点击按钮

四、Button

1. 设计Button按钮的边框为圆角,并对指针悬停时的颜色进行设置

<Window x:Class="Button_Coner.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:Button_Coner"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <!-- 定义带有悬停效果的按钮样式 -->
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="border" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="20">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <!-- 悬停时改变背景颜色 -->
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="LightCoral"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Button Width="150" Height="50" Content="圆角按钮" Background="LightBlue"/>
        <Button Width="100" Height="50" Content="测试" BorderBrush="Green" BorderThickness="2" HorizontalAlignment="Left"></Button>
    </Grid>
</Window>

样式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/975276.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

javaEE-14.spring MVC练习

目录 1.加法计算器 需求分析: 前端页面代码: 后端代码实现功能: 调整前端页面代码: 进行测试: 2.用户登录 需求分析: 定义接口: 1.登录数据校验接口: 2.查询登录用户接口: 前端代码: 后端代码: 调整前端代码: 测试/查错因 后端: 前端: lombok工具 1.引入依赖…

[实现Rpc] 服务端 | RpcRouter实现 | Builder模式

目录 项目服务端独用类的实现 1. RpcRouter类的实现 ServiceDescribe SDescribeFactory ⭕ Builder模式 1. 动机 2. 模式定义 3. 要点总结 4. 代码感受 ServiceManager RpcRouter 4. 代码感受 ServiceManager RpcRouter 前文我们就将 Rpc 通用类都实现完啦&#…

js 实现隔行幻色

构建界面&#xff1a;html&#xff1a; <table cellspacing"0"><!-- 表格中的单元格设为0&#xff0c;没间距 --><thead><tr><td>序号</td><td>姓名</td><td>操作</td></tr></thead><tb…

影刀RPA中级证书-Excel进阶-开票清单

1.操作题需求 请参照视频内容&#xff0c;将开票账单表格中的数据整理成开票清单。请下载 开票账单.xlsx 整理规则如下&#xff1a; 1. 金额为0的数据为赠品&#xff0c;无需开票&#xff0c;需删除2. 开票清单需要从开票账单中获取的数据包括有开票名称、数量、金额、税率&…

tortoiseGit的使用和上传拉取

tortoiseGit的使用和上传拉取 下载TortoiseGit 通过网盘分享的文件&#xff1a;tortoiseGit.zip 链接: https://pan.baidu.com/s/1EOT_UsM9_OysRqXa8gES4A?pwd1234 提取码: 1234 在电脑桌面新建文件夹并进入 右击鼠标 将网址复制上去 用户名和密码是在git注册的用户名和…

从0到1:固件分析

固件分析 0x01 固件提取 1、从厂商官网下载 例如D-link的固件&#xff1a; https://support.dlink.com/resource/products/ 2、代理或镜像设备更新时的流量 发起中间人攻击MITM #启用IP转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward#配置iptables&#xff0c;将目…

JVM预热

阿里电商平台每年的各种大促活动&#xff0c;对于Java技术来说&#xff0c;其中重要一个操作环节就是预热操作。 目录 预热是什么&#xff1f;为什么要预热&#xff1f; java 程序不预热和预热的调用对比 预热是什么&#xff1f; 预热是指&#xff0c;在 JVM 启动后&#xff0…

Datawhale Ollama教程笔记5

Dify 接入 Ollama 部署的本地模型 Dify 支持接入 Ollama 部署的大型语言模型推理和 embedding 能力。 快速接入 下载 Ollama 访问 Ollama 安装与配置&#xff0c;查看 Ollama 本地部署教程。 运行 Ollama 并与 Llama 聊天 ollama run llama3.1Copy to clipboardErrorCopied …

springboot+dubbo+zookeeper的注册服务和调用实践

目录 zookeeper为什么可作为注册中心zookeeper注册中心优缺点启动zookeeper编写springboot项目提供dubbo服务1. 服务接口2. Springboot引入dubbo实现服务接口2.1 工程目录和依赖2.2 启动程序和application.properties2.3 DubboService 实现服务接口2.4 测试api&#xff0c;用于…

学习经验分享【39】YOLOv12——2025 年 2 月 19 日发布的以注意力为核心的实时目标检测器

YOLO算法更新速度很快&#xff0c;已经出到V12版本&#xff0c;后续大家有想发论文或者搞项目可更新自己的baseline了。 代码&#xff1a;GitHub - sunsmarterjie/yolov12: YOLOv12: Attention-Centric Real-Time Object Detectors 摘要&#xff1a;长期以来&#xff0c;增强 …

什么是方法

System.out.println(),那么它是什么呢&#xff1f; Java方法是语句的集合&#xff0c;它们在一起执行一个功能。 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建&#xff0c;在其他地方被使用 这段Java代码出现错误的原因在于&#xff0c;在…

装win10系统提示“windows无法安装到这个磁盘,选中的磁盘采用GPT分区形式”解决方法

问题描述 我们在u盘安装原版win10 iso镜像时&#xff0c;发现在选择硬盘时提示了“windows无法安装到这个磁盘,选中的磁盘采用GPT分区形式”&#xff0c;直接导致了无法继续安装下去。出现这种情况要怎么解决呢&#xff1f; 原因分析&#xff1a; 当您在安装Windows操作系统…

ue5.2.1 quixel brideg显示asset not available in uAsset format

我从未见过如此傻x的bug&#xff0c;在ue5.2.1上通过内置quixel下载资源显示 asset not available in uAsset format 解决办法&#xff1a;将ue更新到最新版本&#xff0c;通过fab进入商场选择资源后add to my library 点击view in launcher打开epic launcher&#xff0c;就可…

python: SQLAlchemy (ORM) Simple example using SQLite

领域层&#xff08;Domain Laye&#xff09;&#xff1a;定义了 School 实体类和 SchoolRepository 抽象基类&#xff0c;明确了业务实体和数据访问的契约。 基础设施层&#xff08;Infrastructure Laye&#xff09;&#xff1a;通过 SQLAlchemy 实现了 SchoolRepository 类&am…

蓝桥杯定时器实现led闪烁

step1.配置定时器&#xff0c;TIM1时高级定时&#xff0c;TIM2是通用定时器&#xff0c;用TIM2就行&#xff0c;用内部时钟源&#xff0c;记住相关公式&#xff0c;定时器中断配置时要使能&#xff0c;且生成代码后也要在mian中写使能函数 step2.写代码 配置生成代码后多出的…

【深度学习】Pytorch的深入理解和研究

一、Pytorch核心理解 PyTorch 是一个灵活且强大的深度学习框架&#xff0c;广泛应用于研究和工业领域。要深入理解和研究 PyTorch&#xff0c;需要从其核心概念、底层机制以及高级功能入手。以下是对 PyTorch 的深入理解与研究的详细说明。 1. 概念 动态计算图&#xff08;D…

HAProxy介绍与编译安装

目录 1、HAProxy介绍 2、HAProxy编译安装 Centos 基础环境 Ubuntu 基础环境 编译安装HAProxy 验证HAProxy版本 HAProxy启动脚本 配置文件 启动haproxy 验证haproxy状态 查看haproxy的状态页面 1、HAProxy介绍 HAProxy是法国开发者 威利塔罗(Willy Tarreau) 在2000年…

动静态链接与加载

目录 静态链接 ELF加载与进程地址空间&#xff08;静态链接&#xff09; 动态链接与动态库加载 GOT表 静态链接 对于多个.o文件在没有链接之前互相是不知到对方存在的&#xff0c;也就是说这个.o文件中调用函数的的跳转地址都会被设定为0&#xff08;当然这个函数是在其他.…

无人机遥控器接口作用详解!

USB接口&#xff1a; 功能&#xff1a;USB接口是一种通用串行总线接口&#xff0c;用于连接外部设备&#xff0c;如手机、平板、电脑或充电设备。在无人机遥控器上&#xff0c;USB接口通常用于数据传输和充电。 应用&#xff1a;用户可以通过USB接口将遥控器与电脑连接&#…

A100、H100、H800、H20等多种显卡配置对比

显卡对比 型号A10080GB SXMA10080GB PCIeH10080GB SXMH10080GB PCIeH20H80080GB SXMH80080GB PCIe数据来源链接链接链接链接链接链接链接GPU架构AmpereAmpereHopperHopperHopperHopperHopper显存容量80GB HBM2e80GB HBM2e80GB94GB96GB80GB80GB显存带宽1,935 GB/s2,039 GB/s3.3…