Click here to Skip to main content
15,885,980 members
Articles / Web Development / ASP.NET

Signum Framework Principles

Rate me:
Please Sign up or sign in to vote.
4.74/5 (27 votes)
25 Jul 2011CPOL18 min read 98.9K   1.1K   86  
Explains the philosophy behind Signum Framework, an ORM with a full LINQ Provider that encourages an entities-first approach.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Signum.Windows
{
    [TemplatePart(Name = "PART_Button", Type = typeof(ToggleButton))]
    [TemplatePart(Name = "PART_Popup", Type = typeof(Popup))]
    public class PickerBase : Control
    {
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(PickerBase), new FrameworkPropertyMetadata(true));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        public static readonly DependencyProperty IsDropDownOpenProperty =
            DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(PickerBase), new FrameworkPropertyMetadata(false));
        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set { SetValue(IsDropDownOpenProperty, value); }
        }

        public static readonly DependencyProperty ButtonContentProperty =
            DependencyProperty.Register("ButtonContent", typeof(object), typeof(PickerBase), new FrameworkPropertyMetadata(null, (d, e) => ((PickerBase)d).ButtonOrPopupContentChanged(e)));
        public object ButtonContent
        {
            get { return (object)GetValue(ButtonContentProperty); }
            set { SetValue(ButtonContentProperty, value); }
        }

        public static readonly DependencyProperty PopupContentProperty =
            DependencyProperty.Register("PopupContent", typeof(object), typeof(PickerBase), new FrameworkPropertyMetadata(null, (d, e) => ((PickerBase)d).ButtonOrPopupContentChanged(e)));
        public object PopupContent
        {
            get { return (object)GetValue(PopupContentProperty); }
            set { SetValue(PopupContentProperty, value); }
        }


        public static readonly RoutedEvent DropDownClosedEvent = EventManager.RegisterRoutedEvent(
            "DropDownClosed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PickerBase));
        public event RoutedEventHandler DropDownClosed
        {
            add { AddHandler(DropDownClosedEvent, value); }
            remove { RemoveHandler(DropDownClosedEvent, value); }
        }

        protected virtual void OnDropDownClosed(EventArgs e)
        {
            base.RaiseEvent(new RoutedEventArgs(DropDownClosedEvent));
        }


        public static readonly RoutedEvent DropDownOpenedEvent = EventManager.RegisterRoutedEvent(
            "DropDownOpened", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PickerBase));
        public event RoutedEventHandler DropDownOpened
        {
            add { AddHandler(DropDownOpenedEvent, value); }
            remove { RemoveHandler(DropDownOpenedEvent, value); }
        }

        protected virtual void OnDropDownOpened(EventArgs e)
        {
            base.RaiseEvent(new RoutedEventArgs(DropDownOpenedEvent));
        }

        static PickerBase()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PickerBase), new FrameworkPropertyMetadata(typeof(PickerBase)));
        }

        private void ButtonOrPopupContentChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)base.RemoveLogicalChild(e.OldValue);
            if (e.NewValue != null)base.AddLogicalChild(e.NewValue);
        }

        ToggleButton toggleButton;
        Popup popup; 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (this.popup != null)
            {
                popup.Closed -= new EventHandler(OnPopupClosed);
                popup.Opened -= new EventHandler(OnPopupOpened);
            }

            toggleButton = (ToggleButton)base.GetTemplateChild("PART_Button");
            popup = (Popup)base.GetTemplateChild("PART_Popup");

            if (this.popup != null)
            {
                popup.Closed += new EventHandler(OnPopupClosed);
                popup.Opened += new EventHandler(OnPopupOpened);
            }
        }

        void OnPopupOpened(object sender, EventArgs e)
        {
            Mouse.Capture(this, CaptureMode.SubTree);

            this.OnDropDownOpened(EventArgs.Empty);
        }

        void OnPopupClosed(object source, EventArgs e)
        {
            if (Mouse.Captured == this)
            {
                Mouse.Capture(this, CaptureMode.None);
            }

            this.OnDropDownClosed(EventArgs.Empty);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            if (Mouse.Captured == this && e.OriginalSource == this)
            {
                popup.IsOpen = false;
            }

            base.OnMouseDown(e);
        }

        //protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        //{
        //    popup.IsOpen = false;

        //    base.OnLostKeyboardFocus(e);
        //}

       


        private void KeyDownHandler(KeyEventArgs e)
        {
            bool flag = false;
            Key systemKey = e.Key;
            if (systemKey == Key.System)
            {
                systemKey = e.SystemKey;
            }
            switch (systemKey)
            {
                case Key.F4:
                    if ((e.KeyboardDevice.Modifiers & ModifierKeys.Alt) == ModifierKeys.None)
                    {
                        IsDropDownOpen = true;
                        flag = true;
                    }
                    break;    

                case Key.Escape:
                    if (this.IsDropDownOpen)
                    {
                        IsDropDownOpen = false;
                        flag = true;
                    }
                    break;         

                case Key.Return:
                    if (this.IsDropDownOpen)
                    {
                        IsDropDownOpen = false;
                        flag = true;
                    }
                    break;

                default:
                    flag = false; break;
            }
         
            if (flag)
            {
                e.Handled = true;
            }
        }
    }
}

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) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions