MainWindow.xaml
<Window x:Class="WpfApp3.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:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="400">
<Grid Background="AliceBlue" >
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="请选择单位类别" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" />
<ComboBox x:Name="combobox" Grid.Row="0" Grid.Column="1" Height="20" Width="90" HorizontalAlignment="Left" SelectionChanged="CBselectionChanged" >
<ComboBoxItem Content="长度" HorizontalAlignment="Left" Width="90"/>
<ComboBoxItem Content="重量" HorizontalAlignment="Left" Width="90"/>
<ComboBoxItem Content="温度" HorizontalAlignment="Left" Width="90"/>
</ComboBox>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="请输入原始数据" HorizontalAlignment="Right" Grid.Column="0" />
<TextBox x:Name="textbox" Grid.Column="1" HorizontalAlignment="Left" Width="90" Height="20" Background="White" TextChanged="TextChanged"/>
<Label Content="{Binding ElementName=listbox1,Path=SelectedItem}"
HorizontalAlignment="Right" Grid.Column="1" />
</Grid>
<Grid Grid.Row="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="换算的结果为" HorizontalAlignment="Right" Grid.Column="0" />
<TextBlock x:Name="tb_res" Height="20" Width="90" Background="White" Grid.Column="1" HorizontalAlignment="Left"
/>
<Label Content="{Binding ElementName=listbox2,Path=SelectedItem}"
HorizontalAlignment="Right" Grid.Column="1" />
</Grid>
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label Content="原始单位" HorizontalAlignment="Center" Grid.Column="0"/>
<ListBox x:Name="listbox1" Grid.Column="0" Margin="5,30,5,5" SelectionChanged="LBselectionChanged"/>
<Label Content="换算单位" HorizontalAlignment="Center" Grid.Column="2"/>
<ListBox x:Name="listbox2" Grid.Column="2" Margin="5,30,5,5" SelectionChanged="LBselectionChanged"/>
<Button Width="100" Height="40" Grid.Column="1">
<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>
</Grid>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
//private ListBox listbox
public partial class MainWindow : Window
{
Dictionary<string, double> MyLength = new Dictionary<string, double>
{ {"m(米)", 1 }, {"cm(厘米)", 0.01 }, {"mm(毫米)", 0.001 }};
Dictionary<string, double> MyWeight = new Dictionary<string, double>
{ {"g(克)", 1 }, {"jin(斤)", 500 }, {"kg(千克)", 1000 }};
private List<string> Mytemp = new List<string>() {"摄氏度","华氏度" };
public MainWindow()
{
InitializeComponent();
}
private void CBselectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb = sender as ComboBox;
ComboBoxItem item = cb.SelectedItem as ComboBoxItem;
string selected = item.Content.ToString();
switch (selected)
{
case "长度":
{
listbox1.ItemsSource = MyLength.Keys;
listbox2.ItemsSource = MyLength.Keys;
break;
}
case "重量":
{
listbox1.ItemsSource = MyWeight.Keys;
listbox2.ItemsSource = MyWeight.Keys;
break;
}
case "温度":
{
listbox1.ItemsSource = Mytemp;
listbox2.ItemsSource = Mytemp;
break;
}
}
}
private void TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (listbox1.SelectedIndex == -1 || listbox2.SelectedIndex == -1) return;
try
{
double src = double.Parse(textBox.Text.ToString());
switch (combobox.SelectedIndex)
{
case 0:
{
tb_res.Text = (src * MyLength[listbox1.SelectedItem.ToString()]/
MyLength[listbox2.SelectedItem.ToString()]).ToString();
break;
}
case 1:
{
tb_res.Text = (src * MyWeight[listbox1.SelectedItem.ToString()] /
MyWeight[listbox2.SelectedItem.ToString()]).ToString();
break;
}
case 2:
{
if (listbox1.SelectedIndex == 0)
{
if (listbox2.SelectedIndex == 0) tb_res.Text = textbox.Text;
else tb_res.Text = (src * 1.8 + 32).ToString();
}
else
{
if(listbox2.SelectedIndex==0) tb_res.Text = ((src - 32) / 1.8).ToString();
else tb_res.Text = textbox.Text;
}
break;
}
}
}
catch(Exception)
{
MessageBox.Show("格式错误", "ERROR");
}
}
private void LBselectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox list = sender as ListBox;
if (textbox.Text.ToString() == "") return;
if (listbox1.SelectedIndex == -1 || listbox2.SelectedIndex == -1) return;
double src = double.Parse(textbox.Text.ToString());
try
{
switch (combobox.SelectedIndex)
{
case 0:
{
tb_res.Text = (src * MyLength[listbox1.SelectedItem.ToString()] /
MyLength[listbox2.SelectedItem.ToString()]).ToString();
break;
}
case 1:
{
tb_res.Text = (src * MyWeight[listbox1.SelectedItem.ToString()] /
MyWeight[listbox2.SelectedItem.ToString()]).ToString();
break;
}
case 2:
{
if (listbox1.SelectedIndex == 0)
{
if (listbox2.SelectedIndex == 0) tb_res.Text = textbox.Text;
else tb_res.Text = (src * 1.8 + 32).ToString();
}
else
{
if (listbox2.SelectedIndex == 0) tb_res.Text = ((src - 32) / 1.8).ToString();
else tb_res.Text = textbox.Text;
}
break;
}
}
}
catch(Exception)
{
MessageBox.Show("格式错误", "ERROR");
}
}
}
}
参考文献
WPF入门教程系列十五——WPF中的数据绑定(一)
WPF编程,C#中弹出式对话框 MessageBox 的几种用法。
C#——《C#语言程序设计》实验报告——Windows桌面编程