|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.