【C#】WPF制作简易转换器

在这里插入图片描述
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桌面编程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值