Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / WPF

Equation Calculator with Graphing

Rate me:
Please Sign up or sign in to vote.
4.92/5 (69 votes)
25 Nov 2010CPOL9 min read 132.7K   4.2K   158  
Equation Calculator with Graphing
<UserControl x:Class="Calculator.CalculatorCtrl"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            xmlns:shared="clr-namespace:CommonUtils;assembly=CommonUtils"
            xmlns:local="clr-namespace:Calculator"
            mc:Ignorable="d" 
            d:DesignHeight="325" d:DesignWidth="471">
    <UserControl.Resources>

        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <!-- 
http://blogs.msdn.com/b/wpfsdk/archive/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox.aspx
-->
                <!--SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/-->
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#fF242424"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF545454"/>
            </Style.Resources>
            <Setter Property="Foreground" Value="{StaticResource Forecolor}" />
        </Style>

        <DataTemplate DataType="{x:Type shared:EquationParser}">
            <shared:DockWrapPanel Margin="0,-2,0,-2">
                <Label Content="{Binding Equation}" VerticalContentAlignment="Center" Margin="0,0,10,0"/>
                <Label Content="{Binding UsedVarsAsString}" 
                    VerticalContentAlignment="Bottom" FontSize="10" />
                <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" FlowDirection="RightToLeft">
                    <Label Content="{Binding ValueAsString}" MinWidth="20" VerticalContentAlignment="Center" FlowDirection="LeftToRight"/>
                    <Label Content="=" VerticalContentAlignment="Center"/>
                </StackPanel>
            </shared:DockWrapPanel>
        </DataTemplate>

        <SolidColorBrush x:Key="infocolor" Color="Wheat" />
        <shared:VisibilityConverter x:Key="visConv" />

    </UserControl.Resources>
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="214*" />
            <RowDefinition Height="101" />
        </Grid.RowDefinitions>
        <ListBox ItemsSource="{Binding Stack}" HorizontalContentAlignment="Stretch" 
            ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            x:Name="m_stackView" Background="Black" TabIndex="4" PreviewKeyDown="OnListboxPreviewKeyDown" />
        <StackPanel Grid.Row="1" Margin="10,0,10,0">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Expression" FontFamily="Calibri" FontSize="14" 
                    Foreground="{StaticResource infocolor}" VerticalAlignment="Center"/>
                <TextBlock Margin="8,0,0,0" FontFamily="Calibri" FontSize="12" 
                    Foreground="{StaticResource infocolor}" VerticalAlignment="Center"
                    Visibility="{Binding ElementName=m_input, Path=IsFocused, Converter={StaticResource visConv}}">
                    Enter expression, &lt;Enter&gt; to calculate
                </TextBlock>
            </StackPanel>
            <DockPanel>
                <Button Margin="3,0,0,0" Width="50" VerticalAlignment="Stretch" Click="OnInsertFunction"
                    Style="{StaticResource StyleBlackButton}" TabIndex="2" DockPanel.Dock="Right" x:Name="m_funcButton">Func</Button>
                <Button Margin="3,0,0,0" Width="50" VerticalAlignment="Stretch" Click="OnCalculate"
                    Style="{StaticResource StyleBlackButton}" TabIndex="1" DockPanel.Dock="Right">Calc</Button>
                <shared:TextBoxWithHistory Text=""
                    Name="m_input" 
                    VerticalAlignment="Top"
                    HorizontalAlignment="Stretch"
                    FontSize="16"
                    KeyDown="OnInputKeyDown"
                    SelectionBrush="LightGray"
                    CaretBrush="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" TabIndex="0">
                </shared:TextBoxWithHistory>
            </DockPanel>
            <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                <TextBlock Text="Constants" FontFamily="Calibri" FontSize="14" 
                    Foreground="{StaticResource infocolor}" VerticalAlignment="Center"/>
                <TextBlock Margin="8,0,0,0" FontFamily="Calibri" FontSize="12" 
                    Foreground="{StaticResource infocolor}" VerticalAlignment="Center"
                    Visibility="{Binding ElementName=m_variables, Path=IsFocused, Converter={StaticResource visConv}}">
                    Semicolon seperated list e.g. 'x=10; y=pi/2'
                </TextBlock>
            </StackPanel>
            <Border>
                <Border.Resources>
                    <Style x:Key="DropButtonStyle" TargetType="Button" BasedOn="{StaticResource StyleBrownButton}">
                    </Style>
                </Border.Resources>
                <shared:TextBoxWithHistory Text="" HorizontalAlignment="Stretch" 
                        Name="m_variables" 
                        VerticalAlignment="Top"
                        FontSize="12"
                        SelectionBrush="LightGray"
                        CaretBrush="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" 
                        TabIndex="3">
                    <shared:TextBoxWithHistory.Resources>
                    </shared:TextBoxWithHistory.Resources>
                </shared:TextBoxWithHistory>
            </Border>
        </StackPanel>
    </Grid>
</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions