WPF实现简易计算器(MVVM、控件自定义样式)
WPF实现简易计算器(MVVM、控件自定义样式)
运行环境:VS2022 .Net framework4.8
完整项目:Gitee仓库
界面
界面如下图所示
文件结构
文件结构:
MVVM实现: Calculator_Model、Calculator_ViewModel、RelayCommond、MainWindow
控件自定义样式:CustomButton、CustomButtonStyle
详细如下图所示
项目代码
MainWindow.xaml
<Window x:Class="MyCalculator.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:MyCalculator"
mc:Ignorable="d"
Title="我的计算器" Height="400" Width="300" MinHeight="400" MinWidth="300">
<Grid Background="#f9f9f9">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="8*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Str1}" HorizontalAlignment="Right" FontSize="15" Foreground="#616161" BorderBrush="{x:Null}" BorderThickness="0" Background="#f9f9f9"/>
<TextBox Grid.Row="1" Text="{Binding Str2}" HorizontalAlignment="Right" FontSize="30" Foreground="Black" BorderBrush="{x:Null}" BorderThickness="0" Background="#f9f9f9"/>
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<local:CustomButton Grid.Row="0" Grid.Column="0" Content="x²" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Powx}"/>
<local:CustomButton Grid.Row="0" Grid.Column="1" Content="√x" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Sqrtx}"/>
<local:CustomButton Grid.Row="0" Grid.Column="2" Content="C" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Clear}"/>
<local:CustomButton Grid.Row="0" Grid.Column="3" Content="⌫" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Backspace}"/>
<local:CustomButton Grid.Row="1" Grid.Column="0" Content="7" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum7}"/>
<local:CustomButton Grid.Row="1" Grid.Column="1" Content="8" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum8}"/>
<local:CustomButton Grid.Row="1" Grid.Column="2" Content="9" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum9}"/>
<local:CustomButton Grid.Row="1" Grid.Column="3" Content="÷" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Div}"/>
<local:CustomButton Grid.Row="2" Grid.Column="0" Content="4" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum4}"/>
<local:CustomButton Grid.Row="2" Grid.Column="1" Content="5" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum5}"/>
<local:CustomButton Grid.Row="2" Grid.Column="2" Content="6" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum6}"/>
<local:CustomButton Grid.Row="2" Grid.Column="3" Content="×" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Mul}"/>
<local:CustomButton Grid.Row="3" Grid.Column="0" Content="1" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum1}"/>
<local:CustomButton Grid.Row="3" Grid.Column="1" Content="2" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum2}"/>
<local:CustomButton Grid.Row="3" Grid.Column="2" Content="3" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum3}"/>
<local:CustomButton Grid.Row="3" Grid.Column="3" Content="-" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Sub}"/>
<local:CustomButton Grid.Row="4" Grid.Column="0" Content="0" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding Addnum0}"/>
<local:CustomButton Grid.Row="4" Grid.Column="1" Content="." Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#cccccc" BackgroundHover="#dddddd" BackgroundPress="#eeeeee" Command="{Binding AddDot}"/>
<local:CustomButton Grid.Row="4" Grid.Column="2" Content="=" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Equ}"/>
<local:CustomButton Grid.Row="4" Grid.Column="3" Content="+" Margin="2" FontSize="30" ButtonCornerRadius="10" Background="#b2dbff" BackgroundHover="#c5e4ff" BackgroundPress="#d9edff" Command="{Binding Plus}"/>
</Grid>
</Grid>
</Window>
Calculator_ViewModel.cs
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace MyCalculator
{
internal class Calculator_ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
//Model实例化
private Calculator_Model _CalculatorM = new Calculator_Model();
#region 声明数据
/// <summary>
/// 声明Model的数据
/// </summary>
public double Num1
{
get { return _CalculatorM.Num1; }
set
{
_CalculatorM.Num1 = value;
RaisePropertyChanged("Num1");
}
}
public double Num2
{
get { return _CalculatorM.Num2; }
set
{
_CalculatorM.Num2 = value;
RaisePropertyChanged("Num2");
}
}
public string Flag1
{
get { return _CalculatorM.Flag1; }
set
{
_CalculatorM.Flag1 = value;
RaisePropertyChanged("Flag1");
}
}
public string Flag2
{
get { return _CalculatorM.Flag2; }
set
{
_CalculatorM.Flag2 = value;
RaisePropertyChanged("Flag2");
}
}
public string Str1
{
get { return _CalculatorM.Str1; }
set
{
_CalculatorM.Str1 = value;
RaisePropertyChanged("Str1");
}
}
public string Str2
{
get { return _CalculatorM.Str2; }
set
{
_CalculatorM.Str2 = value;
RaisePropertyChanged("Str2");
}
}
#endregion
/// <summary>
/// 按钮控件函数
/// </summary>
void Btn_Addnum0()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "0";
}
else
{
Str2 = Str2 + "0";
}
}
void Btn_Addnum1()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "1";
}
else
{
Str2 = Str2 + "1";
}
}
void Btn_Addnum2()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "2";
}
else
{
Str2 = Str2 + "2";
}
}
void Btn_Addnum3()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "3";
}
else
{
Str2 = Str2 + "3";
}
}
void Btn_Addnum4()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "4";
}
else
{
Str2 = Str2 + "4";
}
}
void Btn_Addnum5()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "5";
}
else
{
Str2 = Str2 + "5";
}
}
void Btn_Addnum6()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "6";
}
else
{
Str2 = Str2 + "6";
}
}
void Btn_Addnum7()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "7";
}
else
{
Str2 = Str2 + "7";
}
}
void Btn_Addnum8()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "8";
}
else
{
Str2 = Str2 + "8";
}
}
void Btn_Addnum9()
{
if (Str1 != null && Str1.Contains("="))
{
Str1 = null;
Str2 = "9";
}
else
{
Str2 = Str2 + "9";
}
}
void Btn_AddDot()
{
if (Str2 == null)
{
return;
}
else if (!Str2.Contains("."))
{
Str2 = Str2 + ".";
}
}
void Btn_Powx()
{
Flag2 = "pow";
if (Str2 != null && Str1 == null)
{
Str1 = $"pow({Str2})=";
Num1 = double.Parse(Str2);
Str2 = Math.Pow(Num1, 2).ToString();
}
else if (Str2 != null && (!Str1.Contains(Flag2)))
{
Str1 = $"pow({Str2})=";
Num1 = double.Parse(Str2);
Str2 = Math.Pow(Num1, 2).ToString();
}
}
void Btn_Sqrtx()
{
Flag2 = "sqrt";
if (Str2 != null && Str1 == null)
{
Str1 = $"sqrt({Str2})=";
Num1 = double.Parse(Str2);
Str2 = Math.Pow(Num1, 0.5).ToString();
}
else if (Str2 != null && (!Str1.Contains(Flag2)))
{
Str1 = $"sqrt({Str2})=";
Num1 = double.Parse(Str2);
Str2 = Math.Pow(Num1, 0.5).ToString();
}
}
void Btn_Clear()
{
Num1 = 0;
Num2 = 0;
Str1 = null;
Str2 = null;
Flag1 = null;
Flag2 = null;
}
void Btn_Backspace()
{
if (Str2 != null && Str2.Length > 1)
{
Str2 = Str2.Substring(0, Str2.Length - 1);
}
else
{
Str2 = null;
}
}
void Btn_Plus()
{
Flag1 = "+";
if ((Str1 == null && Str2 != null) || Str1.Contains("="))
{
Str1 = Str2 + "+";
Str2 = null;
}
else if (Str1.Contains("-") || Str1.Contains("×") || Str1.Contains("÷"))
{
Str1 = Str1.Substring(0, Str1.Length - 1);
Str1 = Str1 + "+";
}
}
void Btn_Sub()
{
Flag1 = "-";
if ((Str1 == null && Str2 != null) || Str1.Contains("="))
{
Str1 = Str2 + "-";
Str2 = null;
}
else if (Str1.Contains("+") || Str1.Contains("×") || Str1.Contains("÷"))
{
Str1 = Str1.Substring(0, Str1.Length - 1);
Str1 = Str1 + "-";
}
}
void Btn_Mul()
{
Flag1 = "×";
if ((Str1 == null && Str2 != null) || Str1.Contains("="))
{
Str1 = Str2 + "×";
Str2 = null;
}
else if (Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("÷"))
{
Str1 = Str1.Substring(0, Str1.Length - 1);
Str1 = Str1 + "×";
}
}
void Btn_Div()
{
Flag1 = "÷";
if ((Str1 == null && Str2 != null) || Str1.Contains("="))
{
Str1 = Str2 + "÷";
Str2 = null;
}
else if (Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("×"))
{
Str1 = Str1.Substring(0, Str1.Length - 1);
Str1 = Str1 + "÷";
}
}
void Btn_Equ()
{
if (Str1!=null&&Str2!=null&&(!Str1.Contains("=")))
{
if(Str1.Contains("+") || Str1.Contains("-") || Str1.Contains("×") || Str1.Contains("÷"))
{
double ans;
Num1 = double.Parse(Str1.Substring(0, Str1.Length - 1));
Num2 = double.Parse(Str2);
switch (Str1.Substring(Str1.Length - 1, 1))
{
case "+": ans = Num1 + Num2;break;
case "-": ans = Num1 - Num2; break;
case "×": ans = Num1 * Num2; break;
case "÷": ans = Num1 / Num2; break;
default: return;
}
Str1 = Str1 + Str2 + "=";
Str2=ans.ToString();
}
}
}
bool CanLoginExecute()
{
return true;
}
public ICommand Addnum0 => new RelayCommond(Btn_Addnum0, CanLoginExecute);
public ICommand Addnum1 => new RelayCommond(Btn_Addnum1, CanLoginExecute);
public ICommand Addnum2 => new RelayCommond(Btn_Addnum2, CanLoginExecute);
public ICommand Addnum3 => new RelayCommond(Btn_Addnum3, CanLoginExecute);
public ICommand Addnum4 => new RelayCommond(Btn_Addnum4, CanLoginExecute);
public ICommand Addnum5 => new RelayCommond(Btn_Addnum5, CanLoginExecute);
public ICommand Addnum6 => new RelayCommond(Btn_Addnum6, CanLoginExecute);
public ICommand Addnum7 => new RelayCommond(Btn_Addnum7, CanLoginExecute);
public ICommand Addnum8 => new RelayCommond(Btn_Addnum8, CanLoginExecute);
public ICommand Addnum9 => new RelayCommond(Btn_Addnum9, CanLoginExecute);
public ICommand AddDot => new RelayCommond(Btn_AddDot, CanLoginExecute);
public ICommand Powx => new RelayCommond(Btn_Powx, CanLoginExecute);
public ICommand Sqrtx => new RelayCommond(Btn_Sqrtx, CanLoginExecute);
public ICommand Clear => new RelayCommond(Btn_Clear, CanLoginExecute);
public ICommand Backspace => new RelayCommond(Btn_Backspace, CanLoginExecute);
public ICommand Plus => new RelayCommond(Btn_Plus, CanLoginExecute);
public ICommand Sub => new RelayCommond(Btn_Sub, CanLoginExecute);
public ICommand Mul => new RelayCommond(Btn_Mul, CanLoginExecute);
public ICommand Div => new RelayCommond(Btn_Div, CanLoginExecute);
public ICommand Equ => new RelayCommond(Btn_Equ, CanLoginExecute);
}
}
CustomButton.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MyCalculator
{
public class CustomButton:Button
{
//依赖属性
/// <summary>
/// propdp +Tab键
/// </summary>
public CornerRadius ButtonCornerRadius
{
get { return (CornerRadius)GetValue(ButtonCornerRadiusProperty); }
set { SetValue(ButtonCornerRadiusProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonCornerRadius. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonCornerRadiusProperty =
DependencyProperty.Register("ButtonCornerRadius", typeof(CornerRadius), typeof(CustomButton));
public Brush BackgroundHover
{
get { return (Brush)GetValue(BackgroundHoverProperty); }
set { SetValue(BackgroundHoverProperty, value); }
}
// Using a DependencyProperty as the backing store for BackgroundHover. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundHoverProperty =
DependencyProperty.Register("BackgroundHover", typeof(Brush), typeof(CustomButton));
public Brush BackgroundPress
{
get { return (Brush)GetValue(BackgroundPressProperty); }
set { SetValue(BackgroundPressProperty, value); }
}
// Using a DependencyProperty as the backing store for BackgroundPress. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundPressProperty =
DependencyProperty.Register("BackgroundPress", typeof(Brush), typeof(CustomButton));
}
}
CustomButtonStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyCalculator">
<Style TargetType="{x:Type local:CustomButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomButton}">
<Border x:Name="ButtonBorder" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding ButtonCornerRadius}">
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<!--触发器-->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBorder" Property="Background" Value="{Binding BackgroundHover,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="ButtonBorder" Property="Background" Value="{Binding BackgroundPress,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
WPF实现简易计算器(MVVM、控件自定义样式)的更多相关文章
- WPF Timeline简易时间轴控件的实现
原文:WPF Timeline简易时间轴控件的实现 效果图: 由于整个控件是实现之后才写的教程,因此这里记录的代码是最终实现后的,前后会引用到其他的一些依赖属性或者代码,需要阅读整篇文章. 1.确定T ...
- WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...
- 【转】WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 日历控件Calendar自定义样式: 日期控件DatePicker自定 ...
- silverlight 控件自定义样式 实现方法
1:在app.xaml中加入需实现的样式,如: <Application.Resources> <Style x:Key="NodeStyle" TargetTy ...
- android 控件自定义样式
一.按钮(Button) 方式1.存在.9图片或图片时 可在drawable文件夹下新建xml文件style_button_one.xml,代码如下 <?xml version=" ...
- WPF控件自定义样式(FasControls)
一.界面预览
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
一.RadioButton / CheckBox 系统自带的RadioButton/CheckBox的样式,注定满足不了实际运用中的情况,有时候自定义自己的样式:此次把自己中工作学习过程中所学到的东西 ...
- 【C#】wpf自定义calendar日期选择控件的样式
原文:[C#]wpf自定义calendar日期选择控件的样式 首先上图看下样式 原理 总览 ItemsControl内容的生成 实现 界面的实现 后台ViewModel的实现 首先上图,看下样式 原理 ...
- WPF Menu控件自定义Style
自定义WPF中Menu控件的样式
- WPF 自定义Button控件及样式
这次通过最近做的小例子说明一下自定义Button控件和样式. 实现的效果为:
随机推荐
- vue学习一(指令1.v-text,v-html,插值表达式{{msg}})
一.1.v-text,v-html,插值表达式{{msg}} 注:v-text解决差值表达式闪烁问题,因为他是属性不是差值表达式 1.1.v-text: 是没有闪烁问题的,会覆盖标签的元素中原本的内容 ...
- Ansible忽略任务失败
在默认情况下,任务失败时会中止剧本任务,不过可以通过忽略失败的任务来覆盖此类行为.在可能出错且不影响全局的段中使用ignore_errors关键词来达到目的. 环境: 受控主机清单文件: [dev] ...
- Linux系统发邮件
Linux系统发送邮件 管理服务器时我们经常需要写一些监测脚本,然后在出问题的时候通过邮件来通知 SMTP SMTP(Simple Mail Transfer Protocol)简易邮件传输通讯协议 ...
- Docker启动关闭所有容器命令
docker中 启动所有的容器命令 docker start $(docker ps -a | awk '{ print $1}' | tail -n +2) docker中 关闭所有的容器命令 do ...
- 入门Dify平台:工作流节点分析
要让智能体在实际应用中表现出色,掌握工作流的使用至关重要.今天,我们将深入探讨Dify平台中的各个节点的功能,了解它们的使用方法以及常见的应用场景.通过对这些节点的全面了解,将能够高效地设计和优化智能 ...
- storm部署文档
背景 这篇笔记原来是记录在印象笔记中的,没有发布到博客中,这次我重新整理一下发布上来,希望给读者以参考. Storm的部署手册 Zookeepr的部署 首先下载安装包:apache-zookeeper ...
- frameset frame 实例和用法
转
看这个比较好
- oracle的order by 中文排序原理
近期发现oracle的order by中文排序并不是完全按照拼音排序的 经过测试发现oracle的order by中文排序是按照中文的ASCII码排序的 查询字符ASCII码 select ascii ...
- python多个数列(列表)合并,合并后取值的方法
有时候需要从一个excel或者多个excel读取多列数据,然后传到后面的步骤内去执行操作 这里就涉及到把数据合并再分割的问题,比如下图excel数据,取出两列手机号和余额 思路,先从目标excel内逐 ...
- 20241105,LeetCode 每日一题,用 Go 实现两数之和的非暴力解法
题目 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案,并且你不能 ...