计算器实现
<pre><code class="language-csharp">&lt;Grid&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height=&quot;5*&quot;&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition&gt;&lt;/RowDefinition&gt;
&lt;RowDefinition&gt;&lt;/RowDefinition&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;TextBox x:Name=&quot;txtName&quot; Grid.ColumnSpan=&quot;4&quot;
BorderThickness=&quot;0 0 0 1&quot;
IsReadOnly=&quot;True&quot;
FontSize=&quot;23&quot;
VerticalAlignment=&quot;Bottom&quot;&gt;
&lt;/TextBox&gt;
&lt;Button Grid.Row=&quot;1&quot; Content=&quot;AC&quot; Margin=&quot;5&quot; Width=&quot;40&quot; Height=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot; Content=&quot;X&quot; Margin=&quot;5&quot; Width=&quot;40&quot; Height=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;1&quot; Grid.Column=&quot;2&quot; Content=&quot;%&quot; Width=&quot;40&quot; Height=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;1&quot; Grid.Column=&quot;3&quot; Content=&quot;/&quot; Width=&quot;40&quot; Height=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;0&quot; Content=&quot;7&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; Content=&quot;8&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;2&quot; Content=&quot;9&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;2&quot; Grid.Column=&quot;3&quot; Content=&quot;*&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;3&quot; Grid.Column=&quot;0&quot; Content=&quot;4&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;3&quot; Grid.Column=&quot;1&quot; Content=&quot;5&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;3&quot; Grid.Column=&quot;2&quot; Content=&quot;6&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;3&quot; Grid.Column=&quot;3&quot; Content=&quot;-&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;4&quot; Grid.Column=&quot;0&quot; Content=&quot;1&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;4&quot; Grid.Column=&quot;1&quot; Content=&quot;2&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;4&quot; Grid.Column=&quot;2&quot; Content=&quot;3&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;4&quot; Grid.Column=&quot;3&quot; Content=&quot;+&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Coral&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;5&quot; Grid.Column=&quot;1&quot; Content=&quot;0&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;5&quot; Grid.Column=&quot;2&quot; Content=&quot;.&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Khaki&quot; Foreground=&quot;Black&quot; Click=&quot;Button_Click_2&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;Button Grid.Row=&quot;5&quot; Grid.Column=&quot;3&quot; Content=&quot;=&quot; Height=&quot;40&quot; Width=&quot;40&quot; Background=&quot;Orange&quot; Foreground=&quot;White&quot; Click=&quot;Button_Click_1&quot;&gt;
&lt;Button.Template&gt;
&lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
&lt;Border CornerRadius=&quot;5&quot; Background=&quot;{TemplateBinding Background}&quot;&gt;
&lt;ContentPresenter HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;&lt;/ContentPresenter&gt;
&lt;/Border&gt;
&lt;/ControlTemplate&gt;
&lt;/Button.Template&gt;
&lt;/Button&gt;
&lt;/Grid&gt;
private void Button_Click(object sender, RoutedEventArgs e)
{
txtName.Clear();
}
//等于
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NCalc.Expression exp = new NCalc.Expression(txtName.Text);
var result = exp.Evaluate();
txtName.Text += (sender as Button).Content + result.ToString() ;
}
//点击数字
private void Button_Click_2(object sender, RoutedEventArgs e)
{
txtName.Text += (sender as Button).Content;
}</code></pre>