Click here to Skip to main content
15,893,790 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 1 – Southwind Entities

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
14 Nov 2012LGPL315 min read 41.5K   2K   52  
Tutorial focused in writing the entities using Signum Framework, a Win/Web LINQ-enabled framework for writing data-centric applications.
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.Diagnostics;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities.Reflection;
using System.Reflection;
using System.ComponentModel;
using Signum.Entities;
using System.Collections;

namespace Signum.Windows
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class EntityLine : EntityBase
    {
        public event Func<string, IEnumerable<Lite>> AutoCompleting;

        public static readonly DependencyProperty AutoCompleteProperty =
            DependencyProperty.Register("AutoComplete", typeof(bool), typeof(EntityLine), new FrameworkPropertyMetadata(true));
        public bool AutoComplete
        {
            get { return (bool)GetValue(AutoCompleteProperty); }
            set { SetValue(AutoCompleteProperty, value); }
        }

        public static readonly DependencyProperty HideAutoCompleteOnLostFocusProperty =
            DependencyProperty.Register("HideAutoCompleteOnLostFocus", typeof(bool), typeof(EntityLine), new UIPropertyMetadata(true));
        public bool HideAutoCompleteOnLostFocus
        {
            get { return (bool)GetValue(HideAutoCompleteOnLostFocusProperty); }
            set { SetValue(HideAutoCompleteOnLostFocusProperty, value); }
        }

        int autoCompleteElements = 5;
        public int AutoCompleteElements
        {
            get { return autoCompleteElements; }
            set { autoCompleteElements = value; }
        }

        public EntityLine()
        {
            InitializeComponent();
        }

        public override void OnLoad(object sender, RoutedEventArgs e)
        {
            base.OnLoad(sender, e);

            if (Common.GetIsReadOnly(this) || Implementations != null && Implementations.IsByAll)
                AutoComplete = false;
        }

        protected override void UpdateVisibility()
        {
            btCreate.Visibility = CanCreate() ? Visibility.Visible : Visibility.Collapsed;
            btFind.Visibility = CanFind() ? Visibility.Visible : Visibility.Collapsed;
            btView.Visibility = CanView() ? Visibility.Visible : Visibility.Collapsed;
            btRemove.Visibility = CanRemove() ? Visibility.Visible : Visibility.Collapsed;
        }


        private IEnumerable autoCompleteTextBox_AutoCompleting(string arg)
        {
            IEnumerable value;
            if (AutoCompleting != null)
                value = AutoCompleting(arg);
            else
                value = Server.FindLiteLike(CleanType, safeImplementations, arg, AutoCompleteElements);  

            return value;
        }

        private void autoCompleteTextBox_SelectedItemChanged(object sender, RoutedEventArgs e)
        {
        
            autoCompleteTextBox.Visibility = Visibility.Hidden;
            cc.Focus();
        }

        private void autoCompleteTextBox_Closed(object sender, CloseEventArgs e)
        {
            if (e.IsCommit)
            {
                SetEntityUserInteraction(Server.Convert(autoCompleteTextBox.SelectedItem, Type));
                autoCompleteTextBox.Visibility = Visibility.Hidden;
                cc.Focus();
            }
            else
            {
                if (e.Reason != CloseReason.LostFocus || HideAutoCompleteOnLostFocus)
                    autoCompleteTextBox.Visibility = Visibility.Hidden;
            }
        }

        public void ActivateAutoComplete()
        {
            if (AutoComplete && autoCompleteTextBox.Visibility != Visibility.Visible)
            {
                autoCompleteTextBox.Visibility = Visibility.Visible;
                autoCompleteTextBox.Text = Entity.TryCC(a => a.ToString());
                autoCompleteTextBox.SelectAndFocus();
            }
        }

        private void cc_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F2)
            {
                ActivateAutoComplete();
                e.Handled = true;
            }
        }

        bool doubleClicked = false;
        private void cc_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            doubleClicked = true; 
            ActivateAutoComplete();
        }

        private void cc_GotFocus(object sender, RoutedEventArgs e)
        {
            if (Entity == null)
            {
                ActivateAutoComplete();
                e.Handled = true;
            }
        }

        //private void autoCompleteTextBox_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Escape)
        //    {
        //        autoCompleteTextBox.Visibility = Visibility.Hidden;
        //        autoCompleteTextBox.Close();
        //        e.Handled = true; 
        //    }
        //}

        private void cc_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (doubleClicked)
            {
                doubleClicked = false;
                return;
            }

            if (!cc.Focus())
                ActivateAutoComplete();
        }

    }
}

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 GNU Lesser General Public License (LGPLv3)


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