设置按钮动作的主要方式是使用命令。在 WPF 上具有 GUI 的典型解决方案中,通常会实现 MVVM 模式。在这种情况下,命令是在 ViewModel 中设置的。您可以将参数传递给命令以进行进一步处理。我将以非常简化的形式向您展示 ViewModel 还执行 Model 功能的实现。
该示例使用我的 ViewModelBase 实现。您可以将其替换为您方便的任何内容。或者,如果您对我的实现感兴趣,可以从此存储库中获取它:https://github.com/INexteR/NewTrade/tree/main/NewTradeSln/ViewModels
using Simplified;
using System.Collections.ObjectModel;
namespace Core2024.SO.huojian.question78746674
{
public class CalculatorViewModel : ViewModelBase
{
public static ReadOnlyCollection<string> ButtonNames { get; }
= Array.AsReadOnly("C, +/-, %, ÷, ✖, +, -, ., =, 🠔, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...Any Names"
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
public CalculatorViewModel()
{
DisplayedValue = "0";
}
public RelayCommand CalculatorCommand => GetCommand<string>(CalculatorExecute, CalculatorCanExecute);
private bool CalculatorCanExecute(string parameter)
=> ButtonNames.Contains(parameter);
public string DisplayedValue { get => Get<string>(); private set => Set(value); }
private void CalculatorExecute(string parameter)
{
switch (parameter)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
string value = DisplayedValue;
if (DisplayedValue == "0")
value = string.Empty;
DisplayedValue = value + parameter;
break;
case "C":
DisplayedValue = "0";
break;
case "+/-":
if (DisplayedValue != "0")
{
if (DisplayedValue[0] == '-')
DisplayedValue = DisplayedValue.Substring(1);
else
DisplayedValue = "-" + DisplayedValue;
}
break;
// Logic of other buttons
default:
break;
}
}
}
}
<Window x:Class="Core2024.SO.huojian.question78746674.CalculatorWindow"
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:Core2024.SO.huojian.question78746674"
mc:Ignorable="d"
Title="CalculatorWindow" Height="450" Width="800"
DataContext="{DynamicResource vm}">
<Window.Resources>
<local:CalculatorViewModel x:Key="vm"/>
<Style x:Key="base" TargetType="FrameworkElement">
<Setter Property="Margin" Value="5"/>
</Style>
<Style x:Key="button.command" TargetType="Button">
<Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Command" Value="{Binding CalculatorCommand}"/>
<Setter Property="Padding" Value="15 5"/>
</Style>
<Style x:Key="button.white_button" TargetType="Button" BasedOn="{StaticResource button.command}">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Blue"/>
</Style>
<Style x:Key="button.blue_button" TargetType="Button" BasedOn="{StaticResource button.command}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Text="{Binding DisplayedValue, Mode=OneWay}" Style="{StaticResource base}" IsReadOnly="True"
HorizontalContentAlignment="Right"
Padding="15 5"/>
<UniformGrid Columns="4">
<Button Style="{StaticResource button.blue_button}" Content="C"/>
<Button Style="{StaticResource button.blue_button}" Content="+/-"/>
<Button Style="{StaticResource button.blue_button}" Content="%"/>
<Button Style="{StaticResource button.blue_button}" Content="÷"/>
<Button Style="{StaticResource button.white_button}" Content="7"/>
<Button Style="{StaticResource button.white_button}" Content="8"/>
<Button Style="{StaticResource button.white_button}" Content="9"/>
<Button Style="{StaticResource button.blue_button}" Content="✖"/>
<Button Style="{StaticResource button.white_button}" Content="4"/>
<Button Style="{StaticResource button.white_button}" Content="5"/>
<Button Style="{StaticResource button.white_button}" Content="6"/>
<Button Style="{StaticResource button.blue_button}" Content="+"/>
<Button Style="{StaticResource button.white_button}" Content="1"/>
<Button Style="{StaticResource button.white_button}" Content="2"/>
<Button Style="{StaticResource button.white_button}" Content="3"/>
<Button Style="{StaticResource button.blue_button}" Content="-" FontSize="25"/>
<Button Style="{StaticResource button.white_button}" Content="·"/>
<Button Style="{StaticResource button.white_button}" Content="0"/>
<Button Style="{StaticResource button.blue_button}" Content="🠔"/>
<Button Style="{StaticResource button.blue_button}" Content="="/>
</UniformGrid>
</StackPanel>
</Window>