Click here to Skip to main content
15,892,927 members
Articles / Desktop Programming / WPF

WPF Form Designer Prototype (MVVM)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (31 votes)
3 Apr 2013CPOL9 min read 79.6K   3.4K   108  
Form designer with editable display properties and bindings. Generates XAML forms.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Xml.Serialization;
using SimpleMvvmToolkit;
using Brush = System.Drawing.Brush;

namespace WPFFormDesigner.WPFPropertyGridProxies
{
    public class CheckBoxProxy : WPFControlBindingModelBase, IDisposable
    {
        #region Fields

        private CheckBox component;
        private DependencyPropertyDescriptor dpdHeight;
        private DependencyPropertyDescriptor dpdWidth;
        private DependencyPropertyDescriptor dpdContent;
        private DependencyPropertyDescriptor dpdFontFamily;
        private DependencyPropertyDescriptor dpdFontStyle;
        private DependencyPropertyDescriptor dpdFontSize;
        private DependencyPropertyDescriptor dpdFontWeight;        
        private DependencyPropertyDescriptor dpdForeground;
        private DependencyPropertyDescriptor dpdIsChecked;

        #endregion

        #region Properties

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

        public CheckBox Component
        {
            get { return component; }
            set { component = value; }
        }

        public double Width
        {
            get { return (double)component.GetValue(CheckBox.WidthProperty); }
            set { component.SetValue(CheckBox.WidthProperty, value); }
        }

        public double Height
        {
            get { return (double)component.GetValue(CheckBox.HeightProperty); }
            set { component.SetValue(CheckBox.HeightProperty, value); }
        }

        public string Content
        {
            get { return component.GetValue(CheckBox.ContentProperty).ToString(); }
            set { component.SetValue(CheckBox.ContentProperty, value); }
        }

        public FontFamily FontFamily
        {
            get { return component.GetValue(CheckBox.FontFamilyProperty) as FontFamily; }
            set { component.SetValue(CheckBox.FontFamilyProperty, value); }
        }

        public FontStyle FontStyle
        {
            get { return (FontStyle)component.GetValue(CheckBox.FontStyleProperty); }
            set { component.SetValue(CheckBox.FontStyleProperty, value); }
        }

        public double FontSize
        {
            get { return (double)component.GetValue(CheckBox.FontSizeProperty); }
            set { component.SetValue(CheckBox.FontSizeProperty, value); }
        }

        public FontWeight FontWeight
        {
            get { return (FontWeight)component.GetValue(CheckBox.FontWeightProperty); }
            set { component.SetValue(CheckBox.FontWeightProperty, value); }
        }

        public SolidColorBrush Foreground
        {
            get { return component.GetValue(CheckBox.ForegroundProperty) as SolidColorBrush; }
            set { component.SetValue(CheckBox.ForegroundProperty, value); }
        }

        public Nullable<bool> IsChecked
        {
            get { return component.GetValue(CheckBox.IsCheckedProperty) as Nullable<bool>; }
            set { component.SetValue(CheckBox.IsCheckedProperty, value); }
        }

        #endregion

        public CheckBoxProxy(CheckBox component)
        {
            if (component == null) throw new ArgumentNullException("component");
            this.component = component;

            CaptureComponent(this.component);
        }

        private void CaptureComponent(CheckBox button)
        {
            dpdHeight = DependencyPropertyDescriptor.FromProperty(CheckBox.HeightProperty, typeof(CheckBox));
            dpdHeight.AddValueChanged(button, OnHeightChanged);

            dpdWidth = DependencyPropertyDescriptor.FromProperty(CheckBox.WidthProperty, typeof(CheckBox));
            dpdWidth.AddValueChanged(button, OnWidthChanged);

            dpdContent = DependencyPropertyDescriptor.FromProperty(CheckBox.ContentProperty, typeof(CheckBox));
            dpdContent.AddValueChanged(button, OnContentChanged);

            dpdFontFamily = DependencyPropertyDescriptor.FromProperty(CheckBox.FontFamilyProperty, typeof(CheckBox));
            dpdFontFamily.AddValueChanged(button, OnFontFamilyChanged);

            dpdFontStyle = DependencyPropertyDescriptor.FromProperty(CheckBox.FontStyleProperty, typeof(CheckBox));
            dpdFontStyle.AddValueChanged(button, OnFontStyleChanged);

            dpdFontSize = DependencyPropertyDescriptor.FromProperty(CheckBox.FontSizeProperty, typeof(CheckBox));
            dpdFontSize.AddValueChanged(button, OnFontSizeChanged);

            dpdFontWeight = DependencyPropertyDescriptor.FromProperty(CheckBox.FontWeightProperty, typeof(CheckBox));
            dpdFontWeight.AddValueChanged(button, OnFontWeightChanged);
            
            dpdForeground = DependencyPropertyDescriptor.FromProperty(CheckBox.ForegroundProperty, typeof(CheckBox));
            dpdForeground.AddValueChanged(button, OnForegroundChanged);

            dpdIsChecked = DependencyPropertyDescriptor.FromProperty(CheckBox.ForegroundProperty, typeof(CheckBox));
            dpdIsChecked.AddValueChanged(button, OnIsCheckedChanged);
        }

        private void ReleaseComponent(CheckBox button)
        {
            if (dpdHeight != null)
            {
                dpdHeight.RemoveValueChanged(button, OnHeightChanged);
                dpdHeight = null;
            }

            if (dpdWidth != null)
            {
                dpdWidth.RemoveValueChanged(button, OnWidthChanged);
                dpdWidth = null;
            }

            if (dpdContent != null)
            {
                dpdContent.RemoveValueChanged(button, OnContentChanged);
                dpdContent = null;
            }

            if (dpdFontFamily != null)
            {
                dpdFontFamily.RemoveValueChanged(button, OnFontFamilyChanged);
                dpdFontFamily = null;
            }

            if (dpdFontStyle != null)
            {
                dpdFontStyle.RemoveValueChanged(button, OnFontStyleChanged);
                dpdFontStyle = null;
            }

            if (dpdFontSize != null)
            {
                dpdFontSize.RemoveValueChanged(button, OnFontSizeChanged);
                dpdFontSize = null;
            }

            if (dpdFontWeight != null)
            {
                dpdFontWeight.RemoveValueChanged(button, OnFontWeightChanged);
                dpdFontWeight = null;
            }

            if (dpdForeground != null)
            {
                dpdForeground.RemoveValueChanged(button, OnForegroundChanged);
                dpdForeground = null;
            }

            if (dpdIsChecked != null)
            {
                dpdIsChecked.RemoveValueChanged(button, OnIsCheckedChanged);
                dpdIsChecked = null;
            }
        }

        private void OnHeightChanged(object sender, EventArgs e)
        {
            //OnPropertyChanged("Height");
            NotifyPropertyChanged(x => Height);
        }

        private void OnWidthChanged(object sender, EventArgs e)
        {
            //OnPropertyChanged("Width");
            NotifyPropertyChanged(x => Width);
        }

        private void OnContentChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => Content);
        }

        private void OnFontFamilyChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontFamily);
        }

        private void OnFontStyleChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontStyle);
        }

        private void OnFontSizeChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontSize);
        }

        private void OnFontWeightChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontWeight);
        }

        private void OnForegroundChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => Foreground);
        }

        private void OnIsCheckedChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => IsChecked);
        }

        #region IDisposable Members

        private bool disposed = false;

        ~CheckBoxProxy()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposed) return;

            if (disposing)
            {
                ReleaseComponent(component);
            }

            disposed = true;
        }

        #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
United States United States
Sr. application developer currently developing desktop and web applications for the specialty insurance sector. Data integration specialist, interested in learning the latest .Net technologies.

Comments and Discussions