大家好我想用C#WPF构建一个科学的计算器。
我想要做的是通过调整UI大小小于或大于特定大小(具有特定宽度和高度)来将表单从简单计算器更改为科学计算器。
是否有关于此功能的文章或示例?
非常感谢。
我想指出我的想法:
就像iPhone上的计算器一样,当我旋转屏幕时,计算器将处于科学模式,因此当我拖动调整计算器大小时,这就是我想要的Windows程序,模式将会改变。
答案 0 :(得分:1)
我认为你正在寻找这个
<Window x:Class="Q4.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:Q4"
mc:Ignorable="d"
Title="Calculator"
WindowStyle="SingleBorderWindow"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize" FontSize="20">
<StackPanel Orientation="Vertical">
<Border BorderBrush="Black" BorderThickness="1" Background="LightGray" HorizontalAlignment="Stretch" Height="60" Margin="5" >
<TextBlock Text="0" TextAlignment="Right" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="5" FontSize="40" />
</Border>
<UniformGrid Columns="4">
<UniformGrid.Resources>
<Style TargetType="Button" >
<Setter Property="Margin" Value="5" />
<Setter Property="Width" Value="60" />
<Setter Property="Height" Value="60" />
</Style>
</UniformGrid.Resources>
<Button Content="CE" />
<Button Content="C" />
<Button Content="SC" Click="Button_Click"/>
<Button Content="÷" />
<Button Content="7" />
<Button Content="8" />
<Button Content="9" />
<Button Content="x" />
<Button Content="4" />
<Button Content="5" />
<Button Content="6" />
<Button Content="-" />
<Button Content="1" />
<Button Content="2" />
<Button Content="3" />
<Button Content="+" />
<Button Content="±" />
<Button Content="0" />
<Button Content="." />
<Button Content="=" />
<Button Content="Sin" x:Name="sinBtn" Visibility="Collapsed"/>
<Button Content="Cos" x:Name="cosBtn" Visibility="Collapsed"/>
<Button Content="Log" x:Name="logBtn" Visibility="Collapsed"/>
<Button Content="e" x:Name="eBtn" Visibility="Collapsed"/>
</UniformGrid>
</StackPanel>
</Window>
代码隐藏
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
sinBtn.Visibility = Visibility.Visible;
cosBtn.Visibility = Visibility.Visible;
logBtn.Visibility = Visibility.Visible;
eBtn.Visibility = Visibility.Visible;
}
}
答案 1 :(得分:0)
这个可能会被标记,因为它不是很清楚你在问什么。如果我理解了问题,那么您需要从窗口中删除宽度和高度参数,并设置 int i = 0;
while(inFile)
{
numbers[i] += num;
i++;
inFile >> num;
}
,WindowStyle
和SizeToContent
:
ResizeMode
结果是一个自动调整内容大小的窗口,无法调整大小:
更新:
哦,好吧,那是非常不同的:)实际上有很多方法可以做到这一点,我将提供一个不做MVVM或其他任何事情的简单方法。您需要做的是在窗口的资源部分添加控制模板,每个模式一个,子控件显示它们:
<Window x:Class="GuiTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Calculator"
WindowStyle="SingleBorderWindow"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize" FontSize="20">
<StackPanel Orientation="Vertical">
<Border BorderBrush="Black" BorderThickness="1" Background="LightGray" HorizontalAlignment="Stretch" Height="60" Margin="5" >
<TextBlock Text="0" TextAlignment="Right" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="5" FontSize="40" />
</Border>
<UniformGrid Columns="4">
<UniformGrid.Resources>
<Style TargetType="Button" >
<Setter Property="Margin" Value="5" />
<Setter Property="Width" Value="60" />
<Setter Property="Height" Value="60" />
</Style>
</UniformGrid.Resources>
<Button Content="CE" />
<Button Content="C" />
<Button Content="<" />
<Button Content="÷" />
<Button Content="7" />
<Button Content="8" />
<Button Content="9" />
<Button Content="x" />
<Button Content="4" />
<Button Content="5" />
<Button Content="6" />
<Button Content="-" />
<Button Content="1" />
<Button Content="2" />
<Button Content="3" />
<Button Content="+" />
<Button Content="±" />
<Button Content="0" />
<Button Content="." />
<Button Content="=" />
</UniformGrid>
</StackPanel>
然后你需要添加一个查看新大小的<Window.Resources>
<ControlTemplate x:Key="Standard">
<TextBlock Text="Standard Mode" />
</ControlTemplate>
<ControlTemplate x:Key="Scientific">
<TextBlock Text="Scientific Mode" />
</ControlTemplate>
</Window.Resources>
<ContentControl x:Name="myPanel" />
处理程序,并为要使用的控件选择合适的模板:
SizeChanged
拖动窗口,您会相应地看到控件的外观发生变化。
答案 2 :(得分:0)
我理解你的问题,
您希望以单一形式显示简单科学的计算器。并且会有一些选项..说按钮..这将关闭/关闭科学计算到简单计算。
所以创建两个面板..
面板1&gt;&gt;保持所有简单的calc控制 面板2&gt;保持所有科学控制。
当您点击按钮打开/关闭简单/科学计算..
按一下按钮点击事件并隐藏一个面板并相应地显示其他面板。并相应调整窗口大小,
execute Check_Name 'Pope', 'Life2Stressfull';
按钮点击事件使用:
<WrapPanel Name="simple" Height="300" Width="300" >
<StackPanel>
<Button Content="1" Height="23" Name="button1" Width="40" />
<Button Content="4" Height="23" Name="button4" Width="40" />
<Button Content="7" Height="23" Name="button7" Width="40" />
</StackPanel>
<StackPanel>
<Button Content="2" Height="23" Name="button2" Width="40" />
<Button Content="5" Height="23" Name="button5" Width="40" />
<Button Content="8" Height="23" Name="button8" Width="40" />
</StackPanel>
</WrapPanel>
<WrapPanel Name="scientific" Height="300" Width="500" >
<StackPanel>
<Button Content="1" Height="23" Name="sbutton1" Width="40" />
<Button Content="4" Height="23" Name="sbutton4" Width="40" />
<Button Content="7" Height="23" Name="sbutton7" Width="40" />
</StackPanel>
<StackPanel>
<Button Content="2" Height="23" Name="sbutton2" Width="40" />
<Button Content="5" Height="23" Name="sbutton5" Width="40" />
<Button Content="8" Height="23" Name="sbutton8" Width="40" />
</StackPanel>
<StackPanel>
<some more controls...>
</StackPanel>
</WrapPanel>
反之亦然