Click here to Skip to main content
15,884,629 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.6K   4.2K   158  
Equation Calculator with Graphing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using CommonUtils;

namespace Calculator
{
	/// <summary>
	/// Interaction logic for FunctionList.xaml
	/// </summary>
	public partial class FunctionList : UserControl
	{
		public FunctionList()
		{
			InitializeComponent();
		}
	}
	public class FunctionListPopup : ListPopup
	{
		public FunctionListPopup()
		{
			List = new ObservableCollection<string>();
			foreach (string f in CommonUtils.EquationParser.FunctionsList)
				List.Add(f);

			Child = new FunctionList();
			((FunctionList)Child).DataContext = this;
			((FunctionList)Child).Loaded += new RoutedEventHandler(OnListLoaded);
			Width = 100;
			Height = 150;
			StaysOpen = false;
		}
		void OnListLoaded(object sender, RoutedEventArgs e)
		{
			AttachListBox();
		}
	}
	public class ListPopup : System.Windows.Controls.Primitives.Popup
	{
		ListBox m_listBox = null;
		public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(string), typeof(ListPopup));
		ObservableCollection<string> m_list;
		public bool PopupResult {get; protected set;}
		public string SelectedItem 
		{
			get { return (string)GetValue(SelectedItemProperty); }
			set { SetValue(SelectedItemProperty, value); }
		}
		public ObservableCollection<string> List 
		{
			get { return m_list;}
			set
			{
				m_list = value;
				DataContext = this;
			}
		}
		public ListPopup()
		{
			SelectedItem = string.Empty;
			Loaded += new RoutedEventHandler(OnLoaded);
		}
		protected virtual void AttachListBox()
		{
			if (m_listBox != null)
				return;
			m_listBox = VisualTreeUtils.FindVisualChildByName<ListBox>(Child, "m_listBox");
			if (m_listBox != null)
			{
				// cool, attach and receive event even if it has been handled (true)
				m_listBox.AddHandler(ListBox.MouseLeftButtonDownEvent,
					(RoutedEventHandler)OnLeftButtonDown,
					true);
			}
		}
		void OnLoaded(object sender, RoutedEventArgs e)
		{
			AttachListBox();
		}
		void OnLeftButtonDown(object sender, RoutedEventArgs e)
		{
			Close(SelectedItem.Length > 0);
		}
		public virtual void Open(string curText)
		{
			SelectedItem = curText;
			IsOpen = true;
			if (m_listBox != null)
			{
				m_listBox.Focus();
				ListBoxItem item = m_listBox.ItemContainerGenerator.ContainerFromItem(m_listBox.SelectedItem) as ListBoxItem;
				if (item != null)
					item.Focus();
				else if (List.Count > 0)
					item = m_listBox.ItemContainerGenerator.ContainerFromItem(List[0]) as ListBoxItem;
				if (item != null)
					item.Focus();
			}
		}
		public virtual void Close(bool result)
		{
			PopupResult = result;
			IsOpen = false;
			UIElement target = PlacementTarget;
			if (target != null)
				target.Focus();
		}
		protected override void OnOpened(EventArgs e)
		{
			PopupResult = false;
			base.OnOpened(e);
		}
		protected override void OnPreviewKeyDown(KeyEventArgs e)
		{
			base.OnPreviewKeyDown(e);
			if (e.Key == Key.Escape)
			{
				Close(false);
				return;
			}
			if (e.Key == Key.Enter)
			{
				Close(true);
				return;
			}
		}
	}
}

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