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

Take MVC to the Next Level in .NET

Rate me:
Please Sign up or sign in to vote.
4.62/5 (11 votes)
30 Apr 2013GPL315 min read 73.1K   858   75  
How to quickly build reusable and flexible WPF, Silverlight, or ASP.NET applications with the powerful Xomega Framework using the best MVVM principles.
// Copyright (c) 2010-2012 Xomega.Net. All rights reserved.

using System.Windows;
using System.Windows.Controls.Primitives;

namespace Xomega.Framework.Binding
{
    /// <summary>
    /// A class that provides data property binding for toggle button WPF elements
    /// (checkboxes, radio buttons etc.).
    /// </summary>
    public class ToggleButtonPropertyBinding : DataPropertyBinding
    {
        /// <summary>
        ///  A static method to register the ToggleButtonPropertyBinding for toggle button WPF elements.
        /// </summary>
        public static void Register()
        {
            Register(typeof(ToggleButton), delegate(object obj)
            {
                ToggleButton tb = obj as ToggleButton;
                return IsBindable(tb) ? new ToggleButtonPropertyBinding(tb) : null;
            });
        }

        /// <summary>
        /// Constructs a new toggle button property binding for the given toggle button.
        /// </summary>
        /// <param name="tglButton">The toggle button to be bound to the property.</param>
        protected ToggleButtonPropertyBinding(ToggleButton tglButton)
            : base(tglButton)
        {
            tglButton.Checked += OnStateChanged;
            tglButton.Unchecked += OnStateChanged;
            tglButton.Indeterminate += OnStateChanged;
        }

        /// <summary>
        /// Updates the property when the state of the toggle button is changed.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        private void OnStateChanged(object sender, RoutedEventArgs e)
        {
            UpdateProperty(((ToggleButton)element).IsChecked);
        }

        /// <summary>
        /// Updates the toggle button based on the given property change.
        /// If the property is not required the toggle button will allow three states
        /// to support an indeterminate state when the property value is not set.
        /// Otherwise turns the toggle button on or off based on the Boolean value of the property.
        /// </summary>
        /// <param name="change">The property change.</param>
        protected override void UpdateElement(PropertyChange change)
        {
            base.UpdateElement(change);
            if (property == null) return;

            if (change.IncludesRequired())
            {
                ((ToggleButton)element).IsThreeState = !property.Required;
            }
            if (change.IncludesValue())
            {
                bool? check = null;
                object val = property.InternalValue;
                if (val is bool) check = (bool)val;
                else check = val as bool?;
                ((ToggleButton)element).IsChecked = check;
            }
        }
    }
}

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 General Public License (GPLv3)


Written By
Architect Xomega.Net
United States United States
Xomega Team is striving to increase productivity and development quality by utilizing Model Driven Development coupled with Code Generation and the best design practices for application development.
We provide MDD tools, code generators and frameworks for Visual Studio and .Net development.
Visit us at http://www.xomega.net
This is a Organisation

1 members

Comments and Discussions