Click here to Skip to main content
15,896,348 members
Articles / Mobile Apps / Windows Phone 7

Tidy Up XAML with the ApexGrid

Rate me:
Please Sign up or sign in to vote.
4.82/5 (30 votes)
31 Jul 2011CPOL5 min read 57.9K   1.1K   49  
A small and neat addition to the Grid control which can tidy up XAML in WPF, Silverlight and WP7
using Apex.MVVM;
namespace MVVMSample
{
    /// <summary>
    /// An example view model that uses our new ViewModel base.
    /// </summary>
    public class MainViewModel : ViewModel
    {
        public MainViewModel()
        {
            //  Create the build name command.
            buildNameCommand = new ViewModelCommand(BuildName, false);
        }

        private void BuildName()
        {
            //  Set the full name.
            FullName = FirstName + " " + SecondName;
        }

        /// <summary>
        /// The first name property.
        /// </summary>
        private NotifyingProperty firstNameProperty = 
            new NotifyingProperty("FirstName", typeof(string), string.Empty);

        /// <summary>
        /// The second name property.
        /// </summary>
        private NotifyingProperty secondNameProperty =
            new NotifyingProperty("SecondName", typeof(string), string.Empty);

        /// <summary>
        /// The full name property.
        /// </summary>
        private NotifyingProperty fullNameProperty =
            new NotifyingProperty("FullName", typeof(string), string.Empty);

        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public string FirstName
        {
            get { return (string)GetValue(firstNameProperty); }
            set 
            { 
                SetValue(firstNameProperty, value);
                BuildNameCommand.CanExecute = string.IsNullOrEmpty(FirstName) == false && string.IsNullOrEmpty(SecondName) == false;
            }
        }

        /// <summary>
        /// Gets or sets the second name.
        /// </summary>
        /// <value>The second name.</value>
        public string SecondName
        {
            get { return (string)GetValue(secondNameProperty); }
            set
            {
                SetValue(secondNameProperty, value);
                BuildNameCommand.CanExecute = string.IsNullOrEmpty(FirstName) == false && string.IsNullOrEmpty(SecondName) == false;
            }
        }

        /// <summary>
        /// Gets or sets the full name.
        /// </summary>
        /// <value>The full name.</value>
        public string FullName
        {
            get { return (string)GetValue(fullNameProperty); }
            set { SetValue(fullNameProperty, value); }
        }

        /// <summary>
        /// The build name command.
        /// </summary>
        private ViewModelCommand buildNameCommand;

        /// <summary>
        /// Gets the build name command.
        /// </summary>
        /// <value>The build name command.</value>
        public ViewModelCommand BuildNameCommand
        {
            get { return buildNameCommand; }
        }
    }
}

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
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions