一、實驗目的
- 熟悉使用WPF進行界面編程的基本過程;
- 掌握WPF佈局、控件、事件的使用。
二、實驗內容
- 使用WPF技術,進行合理佈局,設計一個窗體應用程序,完成一些常用度量單位的轉換,基本模板如下圖:
對於本實驗,屬於一個小型的界面,主要設計過程包括:
(1)新建WPF工程。
(2)選擇內容佈局控件。以Grid控件爲主,在其中內嵌其它佈局控件。併爲佈局控件設置一些重要的屬性,如行數與列數,平均分配還是比例分配等。操作方式,主要通過拖放控件,再輔以少量XAML編輯。
(3)選擇各類控件,爲它們安排好位置,設置它們的各類屬性,如顏色、大小、靠左還是靠右、margin、padding等。首先拖放控件,然後在屬性窗口中編輯。
(4)添加事件處理方法。通過事件標籤頁或雙擊控件添加。
另外要求:
- 在原始數據和換算結果後加兩個TextBlock,綁定到原始單位和換算單位。
- 當在TextBox輸入內容時,換算結果內容清空。
- 如果原始數據格式有誤,要提示用戶。
- 可在顏色、佈局、功能等方面按自己的想法進行完善。
其中:
(1)中間的Button用下面的方式面出來:
<Button Width="100" Height="40"> <Canvas> <Path Stroke="Blue" Data="M -30, -5 l 40,0 l0,-6 l 20,10 l-20,10 l0,-6 l-40,0 Z"> </Path> </Canvas> </Button>
(2)爲在後臺代碼中訪問控件,需要給它命名,如
<ListBox x:Name="lstSource"></ListBox>
(3)響應ComboBox的SelectionChanged事件,獲取到選定內容的方法是:
ComboBox cb = sender as ComboBox; ComboBoxItem item = cb.SelectedItem as ComboBoxItem; string selected = item.Content.ToString();
然後通過switch語句分別處理長度、重量等選項。
- 對於長度等單位,可構建如下字典對象輔助轉換:
Dictionary<string, double> length = new Dictionary<string, double>
{ {"m(米)", 1 }, {"cm(釐米)", 0.01 }, {"mm(毫米)", 0.001 }};
但溫度不行,需自行轉換。
三、程序代碼
UI設計
<Window x:Class="Homework10.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:Homework10"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid Loaded="Grid_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Center" TextWrapping="Wrap" Text="請選擇單位類別" VerticalAlignment="Center"/>
<ComboBox Grid.Column="1" Grid.Row="0" x:Name="comboTypes" SelectionChanged="ComboTypes_SelectionChanged">
<ComboBoxItem>長度</ComboBoxItem>
<ComboBoxItem>質量</ComboBoxItem>
<ComboBoxItem>溫度</ComboBoxItem>
</ComboBox>
<TextBlock Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Center" TextWrapping="Wrap" Text="請輸入原始數據" VerticalAlignment="Center"/>
<TextBox x:Name="lstvalText" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="0" TextChanged="Cal_Text"/>
<TextBlock x:Name="lstText" Grid.Row="1" Grid.Column="2"
HorizontalAlignment="Center" TextWrapping="Wrap" Text="原始單位" VerticalAlignment="Center" />
<TextBlock Grid.Row="2" Grid.Column="0"
HorizontalAlignment="Center" TextWrapping="Wrap" Text="換算結果" VerticalAlignment="Center"/>
<TextBox x:Name="tndvalText" Grid.Row="2" Grid.Column="1" TextWrapping="Wrap" Text="0" IsEnabled="False"/>
<TextBlock x:Name="tndText" Grid.Row="2" Grid.Column="2"
HorizontalAlignment="Center" TextWrapping="Wrap" Text="換算單位" VerticalAlignment="Center"/>
<TextBlock x:Name="errText" Grid.Row="3" Grid.ColumnSpan="3" TextWrapping="Wrap" Text="" />
<ListBox x:Name="lstSource" Grid.Row="4" Grid.Column="0" SelectionChanged="LstSource_SelectionChanged"/>
<Button Grid.Column="1" Grid.Row="4" Width="160" Height="40" Click="CalBtn_Click">
<Canvas>
<Path Stroke="Blue" Data="M -30, -5 l 40,0 l0,-6 l 20,10 l-20,10 l0,-6 l-40,0 Z">
</Path>
</Canvas>
</Button>
<ListBox x:Name="tndSource" Grid.Row="4" Grid.Column="2" SelectionChanged="TndSource_SelectionChanged"/>
</Grid>
</Window>
邏輯設計
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 Homework10
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
Dictionary<string, double> length = new Dictionary<string, double>
{ {"m(米)", 1 }, {"cm(釐米)", 0.01 }, {"mm(毫米)", 0.001 }};
Dictionary<string, double> mass = new Dictionary<string, double>
{ {"k(克)", 1 }, {"jin(斤)", 500 }, {"kg(千克)", 1000 }};
List<string> items = new List<string>() { "攝氏度", "華氏度" };
public MainWindow()
{
InitializeComponent();
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
lstSource.ItemsSource = length.Keys;
tndSource.ItemsSource = length.Keys;
comboTypes.SelectedIndex = 0;
}
private void LstSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox1 = sender as ListBox;
string txt = listBox1.SelectedItem as string;
lstText.Text = txt;
}
private void TndSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox2 = sender as ListBox;
string txt = listBox2.SelectedItem as string;
tndText.Text = txt;
}
private void ComboTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
ComboBoxItem item = cb.SelectedItem as ComboBoxItem;
string selected = item.Content.ToString();
if (selected == "長度")
{
lstSource.ItemsSource = length.Keys;
tndSource.ItemsSource = length.Keys;
}
else if (selected == "質量")
{
lstSource.ItemsSource = mass.Keys;
tndSource.ItemsSource = mass.Keys;
}
else if (selected == "溫度")
{
lstSource.ItemsSource = items;
tndSource.ItemsSource = items;
}
}
private void CalBtn_Click(object sender, RoutedEventArgs e)
{
}
private void Cal_Text(object sender, TextChangedEventArgs e)
{
if (lstSource == null || tndSource == null)
return;
if(errText!=null)
errText.Text = "";
if (lstvalText.Text == "" || lstSource.SelectedIndex == -1 || tndSource.SelectedIndex == -1) {
tndvalText.Text = "0";
return;
}
try
{
tndvalText.Text = "0";
double a = double.Parse(lstvalText.Text);
if (comboTypes.SelectedIndex == 0)
{
tndvalText.Text = (a * length[lstText.Text] / length[tndText.Text]).ToString();
}
else if (comboTypes.SelectedIndex == 1)
{
tndvalText.Text = (a * mass[lstText.Text] / mass[tndText.Text]).ToString();
}
else if (comboTypes.SelectedIndex == 2)
{
if (lstSource.SelectedIndex == 0)
{
tndvalText.Text = (a * 1.8 + 32).ToString();
}
else
{
tndvalText.Text = ((a - 32) / 1.8).ToString();
}
}
}
catch (FormatException fe)
{
errText.Text = "數據格式錯誤:" + fe.Message;
}
catch (KeyNotFoundException kfe) {
errText.Text = "單位錯誤:" + kfe.Message;
}
}
}
}
四、運行結果
五、實驗心得與體會
- 熟悉使用WPF進行界面編程的基本過程;
- 掌握WPF佈局、控件、事件的使用。
參考文章
https://baike.baidu.com/item/%E5%8D%8E%E6%B0%8F%E5%BA%A6/9982416?fr=aladdin#2
https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/controls/how-to-get-a-listboxitem
https://blog.csdn.net/weixin_44543135/article/details/98762476