Click here to Skip to main content
15,886,137 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.GraphForm"
             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:local="clr-namespace:Calculator"
             xmlns:graph="clr-namespace:CommonUtils.GraphicalCanvas;assembly=CommonUtils"
             xmlns:shared="clr-namespace:CommonUtils;assembly=CommonUtils"
             mc:Ignorable="d" 
             d:DesignHeight="313" d:DesignWidth="512">
    <UserControl.Resources>

        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <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}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>

        <ControlTemplate x:Key="myTextTemplate" TargetType="TextBoxBase">
            <Border 
                BorderThickness="0.4"
                Background="{TemplateBinding Background}" 
                BorderBrush="{TemplateBinding Foreground}" 
                >
                    <ScrollViewer Name="PART_ContentHost" 
                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
                        HorizontalContentAlignment="Center" 
                        VerticalContentAlignment="Center"/>
            </Border>
        </ControlTemplate>

        <shared:ColorListPopup x:Key="ColorPopup"
                IsOpen="False" 
                StaysOpen="False"
                Placement="Bottom" 
                AllowsTransparency="True"
                Width="200"
                Height="200"
                >
            <shared:ColorListBox x:Name="PART_ColorList" Background="White" ShowDetail="False"></shared:ColorListBox>
        </shared:ColorListPopup>

        <ControlTemplate x:Key="_notSelected" TargetType="{x:Type Control}">
            <StackPanel Orientation="Horizontal" Height="25" >
                <CheckBox IsChecked="{Binding IsEnabled}" VerticalAlignment="Center" Margin="5,0,5,0"/>

                <Button Width="20" Height="8" Click="OnClick" Focusable="False" >
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel>
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="{TemplateBinding Button.Content}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                    <Button.Content>
                        <Border BorderBrush="DarkGray" BorderThickness="0.5" Background="{Binding GraphColor}" 
                                Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
                    </Button.Content>
                </Button>

                <TextBlock Text="{Binding Equation}" Margin="3,3,0,0"
                           />
            </StackPanel>
        </ControlTemplate>

        <ControlTemplate x:Key="_selected" TargetType="{x:Type Control}">
            <DockPanel Height="25">
                <CheckBox IsChecked="{Binding IsEnabled}" VerticalAlignment="Center" Margin="5,0,5,0"/>

                <Button Width="20" Height="8" Click="OnClick" Focusable="False">
                    <Button.Template>
                        <ControlTemplate>
                            <StackPanel>
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="{TemplateBinding Button.Content}" />
                            </StackPanel>
                        </ControlTemplate>
                    </Button.Template>
                    <Button.Content>
                        <Border BorderBrush="DarkGray" BorderThickness="0.5" Background="{Binding GraphColor}" 
                                Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
                    </Button.Content>
                </Button>


                <TextBox Text="{Binding Equation}"
                         x:Name="m_equationEdit"
                         SelectionBrush="LightGray"
                         CaretBrush="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" 
                         Template="{StaticResource myTextTemplate}" VerticalContentAlignment="Center" >
                    <TextBox.Background>
                        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                    </TextBox.Background>
                </TextBox>
            </DockPanel>
        </ControlTemplate>

        <DataTemplate DataType="{x:Type local:GraphItem}">
            <Control x:Name="_container" Template="{StaticResource _notSelected}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                    <Setter TargetName="_container" Property="Template" Value="{StaticResource _selected}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:NewGraphItem}">
            <Border Height="25">
                <Button Click="OnNewClicked"
                        Style="{StaticResource StyleBlackButton}">
                    Click to add new...
                </Button>
            </Border>
        </DataTemplate>
        
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="24" />
            <RowDefinition Height="273*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="318*" />
        </Grid.ColumnDefinitions>
        <Border Grid.Column="1" Grid.Row="1" BorderBrush="DarkGray" BorderThickness="0.5" Margin="3">
            <graph:CanvasCtrl Background="Black" x:Name="m_canvas" >
            </graph:CanvasCtrl>
        </Border>
        <Grid  Grid.Column="1" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="241*" />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <TextBlock x:Name="m_label" FontSize="10" Grid.Row="1" Text="some text"
                       Margin="6,0,0,3"
                       VerticalAlignment="Bottom"
                       Foreground="Wheat"
                       />
        </Grid>
        <ListBox Grid.RowSpan="2" 
                 x:Name="m_itemsListBox"
                 Background="Transparent"
                 ItemsSource="{Binding GraphItems}" 
                 HorizontalContentAlignment="Stretch"
                 SelectionChanged="OnGraphSelectionChanged"
                 />
        <Button Content="Calc" Height="23" HorizontalAlignment="Left" Margin="3,0,0,0" 
                VerticalAlignment="Top" Width="75" Grid.Column="1" Click="OnRecalc" 
                Style="{StaticResource StyleBlackButton}"/>
        <Button Content="Zoom" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="165,0,0,0" VerticalAlignment="Top" Width="75" Click="ZoomToFit"
                Style="{StaticResource StyleBlackButton}"/>
        <Button Content="Center" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="84,0,0,0" VerticalAlignment="Top" Width="75" Click="CenterCanvas"
                Style="{StaticResource StyleBlackButton}"/>
    </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