目錄
一、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>
樣式