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){// 獲取當前時間并更新 TextBoxtimeTextBlock.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的ItemsSourceStudentList.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的ItemsSourcemultiSelectComboBox.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>

樣式

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/70350.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/70350.shtml
英文地址,請注明出處:http://en.pswp.cn/web/70350.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

二級公共基礎之數據結構與算法篇(八)排序技術

目錄 前言 一、交換類排序 1.冒泡排序法 1. 冒泡排序的思想 2. 冒泡排序的實現步驟 3. 示例 4. 冒泡排序的特點 2.快速排序 1. 快速排序的核心思想 2. 快速排序的實現步驟 3. 示例代碼(C語言) 4. 快速排序的特點 二、插入類排序 1. 簡單插入排序 1.簡單插入排…

記錄一次 ALG 的處理過程

前幾天朋友找我幫忙&#xff0c;說碰到很大困難了&#xff0c;實際上&#xff0c;不過如此 現象是這樣的&#xff1a; FreeSWITCH mod_unimrcp 工作不正常 FS 和 mrcp-server 兩邊同時抓包&#xff0c;看到的是&#xff1a; sip 流程正常 FS TCP 連接到 mccp-server 失敗&…

【Linux網絡編程】IP協議格式,解包步驟

目錄 解析步驟 1.版本字段&#xff08;大小&#xff1a;4比特位&#xff09; 2.首部長度&#xff08;大小&#xff1a;4比特位&#xff09;&#xff08;單位&#xff1a;4字節&#xff09; &#x1f35c;細節解釋&#xff1a; 3.服務類型&#xff08;大小&#xff1a;8比特…

CSDN文章質量分查詢系統【贈python爬蟲、提分攻略】

CSDN文章質量分查詢系統 https://www.csdn.net/qc 點擊鏈接-----> CSDN文章質量分查詢系統 <------點擊鏈接 點擊鏈接-----> https://www.csdn.net/qc <------點擊鏈接 點擊鏈接-----> CSDN文章質量分查詢系統 <------點擊鏈接 點擊鏈…

HTML應用指南:利用GET請求獲取全國瀘溪河門店位置信息

隨著新零售業態的快速發展,門店位置信息的獲取變得越來越重要。作為新興烘焙品牌之一,瀘溪河自2013年在南京創立以來,一直堅持“健康美味,香飄世界”的企業使命,以匠人精神打造新中式糕點。為了更好地理解和利用這些數據,本篇文章將深入探討GET請求的實際應用,并展示如何…

如何在 React 中測試高階組件?

在 React 中測試高階組件可以采用多種策略&#xff0c;以下是常見的測試方法&#xff1a; 1. 測試高階組件返回的組件 高階組件本身是一個函數&#xff0c;它返回一個新的組件。因此&#xff0c;可以通過測試這個返回的組件來間接測試高階組件的功能。通常使用 Jest 作為測試…

R語言Stan貝葉斯空間條件自回歸CAR模型分析死亡率多維度數據可視化

全文鏈接&#xff1a;https://tecdat.cn/?p40424 在空間數據分析領域&#xff0c;準確的模型和有效的工具對于研究人員至關重要。本文為區域數據的貝葉斯模型分析提供了一套完整的工作流程&#xff0c;基于Stan這一先進的貝葉斯建模平臺構建&#xff0c;幫助客戶為空間分析帶來…

Casbin 權限管理介紹及在 Go 語言中的使用入門

引言 在現代軟件開發過程中&#xff0c;權限管理是一個至關重要的環節&#xff0c;它關系到系統的安全性和用戶體驗。Casbin 是一個強大的訪問控制庫&#xff0c;支持多種訪問控制模型&#xff0c;如 ACL&#xff08;訪問控制列表&#xff09;、RBAC&#xff08;基于角色的訪問…

快速入門——第三方組件element-ui

學習自嗶哩嗶哩上的“劉老師教編程”&#xff0c;具體學習的網站為&#xff1a;10.第三方組件element-ui_嗶哩嗶哩_bilibili&#xff0c;以下是看課后做的筆記&#xff0c;僅供參考。 第一節 組件間的傳值 組件可以有內部Data提供數據&#xff0c;也可由父組件通過prop方式傳…

【算法通關村 Day7】遞歸與二叉樹遍歷

遞歸與二叉樹遍歷青銅挑戰 理解遞歸 遞歸算法是指一個方法在其執行過程中調用自身。它通常用于將一個問題分解為更小的子問題&#xff0c;通過重復調用相同的方法來解決這些子問題&#xff0c;直到達到基準情況&#xff08;終止條件&#xff09;。 遞歸算法通常包括兩個主要…

樸素貝葉斯法

文章目錄 貝葉斯定理樸素貝葉斯法的學習與分類條件獨立假設樸素貝葉斯的后驗概率最大化準則樸素貝葉斯的基本公式 樸素貝葉斯法的參數估計極大似然估計 貝葉斯定理 前置知識&#xff1a;條件概率、全概率、貝葉斯公式 推薦視頻&#xff0c;看完視頻后搜索博客了解先驗概率、后…

《A++ 敏捷開發》- 20 從 AI 到最佳設計

“我們現在推行AIGC&#xff0c;服務端不需要UI交互設計的用AI自動產出代碼&#xff0c;你建議的結對編程、TDD等是否還適用&#xff1f;” 這兩年AI確實很火&#xff0c;是報紙、雜志的熱門話題。例如&#xff0c;HBR雜志從2024年9月至2025年二月份3期&#xff0c;里面有接近一…

GO系列-IO 文件操作

os io 判斷文件是否存在 func fileExist(filePath string) (bool, error) {_, err : os.Stat(filePath)if err nil {return true, nil}if os.IsNotExist(err) {return false, nil}return false, &CheckFileExistError{filePath} } 讀取文件內容 func readFileContext(…

rs485協議、電路詳解(保姆級)

起源 RS-485即Recommended Standard 485 協議的簡寫。1983年被電子工業協會(EIA)批準為一種通訊接口標準. 數據在通信雙方之間傳輸&#xff0c;本質是傳輸物理的電平&#xff0c;比方說傳輸5V的電壓 -1V的電壓信號&#xff0c;這些物理信號在傳輸過程中會受到很多干擾&#x…

JavaWeb-Tomcat服務器

文章目錄 Web服務器存在的意義關于Web服務器軟件Tomcat服務器簡介安裝Tomcat服務器Tomcat服務器源文件解析配置Tomcat的環境變量啟動Tomcat服務器一個最簡單的webapp(不涉及Java) Web服務器存在的意義 我們之前介紹過Web服務器進行通信的原理, 但是我們當時忘記了一點, 服務器…

【愚公系列】《Python網絡爬蟲從入門到精通》008-正則表達式基礎

標題詳情作者簡介愚公搬代碼頭銜華為云特約編輯,華為云云享專家,華為開發者專家,華為產品云測專家,CSDN博客專家,CSDN商業化專家,阿里云專家博主,阿里云簽約作者,騰訊云優秀博主,騰訊云內容共創官,掘金優秀博主,亞馬遜技領云博主,51CTO博客專家等。近期榮譽2022年度…

視覺分析之邊緣檢測算法

9.1 Roberts算子 Roberts算子又稱為交叉微分算法&#xff0c;是基于交叉差分的梯度算法&#xff0c;通過局部差分計算檢測邊緣線條。 常用來處理具有陡峭的低噪聲圖像&#xff0c;當圖像邊緣接近于正45度或負45度時&#xff0c;該算法處理效果更理想。 其缺點是對邊緣的定位…

DuodooBMS源碼解讀之 sale_change模塊

銷售變更模塊用戶使用手冊 一、模塊概述 本擴展模塊主要包含兩個主要的 Python 文件&#xff1a;sale_change/report/sale_change_report.py 和 sale_change/wizard/sale_change_download.py&#xff0c;提供了銷售變更報表查看和銷售變更單下載的功能。以下是詳細的使用說明…

OpenCV形態學操作

1.1. 形態學操作介紹 初識&#xff1a; 形態學操作是一種基于圖像形狀的處理方法&#xff0c;主要用于分析和處理圖像中的幾何結構。其核心是通過結構元素&#xff08;卷積核&#xff09;對圖像進行掃描和操作&#xff0c;從而改變圖像的形狀和特征。例如&#xff1a; 腐蝕&…

力扣算法-1

力扣算法 1 兩數之和 給定一個整數數組nums和一個整數目標值target&#xff0c;請你在數組中找出和為目標值target的那兩個整數&#xff0c;返回他們的數組下標。 &#xff08;1&#xff09;暴力枚舉 &#xff08;枚舉數組每一個數x&#xff0c;再尋找數組中是否存在 targe…