Click here to Skip to main content
15,884,836 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
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 Signum.Utilities;

namespace Signum.Windows
{
    /// <summary>
    /// Interaction logic for ButtonBar.xaml
    /// </summary>
    public partial class ButtonBar : UserControl
    {
        public static event GetButtonBarElementDelegate GetButtonBarElement;

        public static readonly DependencyProperty MainControlProperty =
            DependencyProperty.Register("MainControl", typeof(Control), typeof(ButtonBar));
        public Control MainControl
        {
            get { return (Control)GetValue(MainControlProperty); }
            set { SetValue(MainControlProperty, value); }
        }

        public static readonly DependencyProperty OkVisibleProperty =
            DependencyProperty.Register("OkVisible", typeof(bool), typeof(ButtonBar), new UIPropertyMetadata(false));
        public bool OkVisible
        {
            get { return (bool)GetValue(OkVisibleProperty); }
            set { SetValue(OkVisibleProperty, value); }
        }

        public static readonly DependencyProperty SaveVisibleProperty =
            DependencyProperty.Register("SaveVisible", typeof(bool), typeof(ButtonBar), new UIPropertyMetadata(false));
        public bool SaveVisible
        {
            get { return (bool)GetValue(SaveVisibleProperty); }
            set { SetValue(SaveVisibleProperty, value); }
        }

        public static readonly DependencyProperty ReloadVisibleProperty =
            DependencyProperty.Register("ReloadVisible", typeof(bool), typeof(ButtonBar), new UIPropertyMetadata(false));
        public bool ReloadVisible
        {
            get { return (bool)GetValue(ReloadVisibleProperty); }
            set { SetValue(ReloadVisibleProperty, value); }
        }

        public event RoutedEventHandler OkClick
        {
            add { btOk.Click += value; }
            remove { btOk.Click -= value; }
        }

        public event RoutedEventHandler SaveClick
        {
            add { btSave.Click += value; }
            remove { btSave.Click -= value; }
        }

        public event RoutedEventHandler ReloadClick
        {
            add { btReload.Click += value; }
            remove { btReload.Click -= value; }
        }

        public Button OkButton
        {
            get { return btOk; }
        }

        public Button SaveButton
        {
            get { return btSave; }
        }

        public Button ReloadButton
        {
            get { return btReload; }
        }

        public ViewButtons ViewButtons { get; set; }

        public ButtonBar()
        {
            InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(ToolBar_DataContextChanged);
        }

        void ToolBar_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            List<FrameworkElement> elements = new List<FrameworkElement>();
            if (GetButtonBarElement != null)
                elements.AddRange(GetButtonBarElement.GetInvocationList()
                    .Cast<GetButtonBarElementDelegate>()
                    .Select(d => d(e.NewValue, MainControl, ViewButtons))
                    .NotNull().SelectMany(d => d).NotNull().ToList());

            stackPanel.Children.Clear();

            foreach (var item in elements)
            {
                stackPanel.Children.Add((UIElement)item);
            }
        }

        public static void Start()
        {
            ButtonBar.GetButtonBarElement += (obj, mainControl, viewButtons) => mainControl is IHaveToolBarElements ?
                ((IHaveToolBarElements)mainControl).GetToolBarElements(obj, viewButtons) : null;
        }
    }

    public delegate List<FrameworkElement> GetButtonBarElementDelegate(object entity, Control mainControl, ViewButtons buttons);

    public interface IHaveToolBarElements
    {
        List<FrameworkElement> GetToolBarElements(object dataContext, ViewButtons buttons);
    }

    public enum ViewButtons
    {
        Ok,
        Save
    }
}

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