Click here to Skip to main content
15,881,638 members
Articles / Desktop Programming / XAML

The Bricks Game for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.92/5 (43 votes)
8 Sep 2010CPOL13 min read 80.1K   1.3K   81  
An article exploring the use of MVVM, styles and templates in game programming in Silverlight 4
//This is the ButtonService class that allows 
//a Silverlight button to have a Command property
//and thus allowing the MVVM pattern in Silverlight.
//
//Implementation by Patrick Cauldwell:
//http://www.cauldwell.net/patrick/blog/MVVMBindingToCommandsInSilverlight.aspx
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightCommands
{
    public static class ButtonService
    {
        #region CommandParameter

        /// <summary>
        /// CommandParameter Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(string), typeof(ButtonService),
                new PropertyMetadata(OnCommandParameterChanged));

        /// <summary>
        /// Gets the CommandParameter property.
        /// </summary>
        public static string GetCommandParameter(DependencyObject d)
        {
            return (string)d.GetValue(CommandParameterProperty);
        }

        /// <summary>
        /// Sets the CommandParameter property.
        /// </summary>
        public static void SetCommandParameter(DependencyObject d, string value)
        {
            d.SetValue(CommandParameterProperty, value);
        }

        /// <summary>
        /// Handles changes to the CommandParameter property.
        /// </summary>
        private static void OnCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        #endregion
        
        #region Command

        /// <summary>
        /// Command Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ButtonService),
                new PropertyMetadata(OnCommandChanged));

        /// <summary>
        /// Gets the Command property.
        /// </summary>
        public static ICommand GetCommand(DependencyObject d)
        {
            return (ICommand)d.GetValue(CommandProperty);
        }

        /// <summary>
        /// Sets the Command property.
        /// </summary>
        public static void SetCommand(DependencyObject d, ICommand value)
        {
            d.SetValue(CommandProperty, value);
        }

        /// <summary>
        /// Handles changes to the Command property.
        /// </summary>
        private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Button)
            {
                string parameter = d.GetValue(CommandParameterProperty) as string;
                Button b = d as Button;
                ICommand c = e.NewValue as ICommand;
                b.Click += delegate(object sender, RoutedEventArgs arg) { c.Execute(parameter); };
            }
        }

        #endregion
        
        
    }
}

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
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions