Click here to Skip to main content
15,896,557 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.7K   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 Brush = System.Drawing.Brush;

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

        private Button component;
        private DependencyPropertyDescriptor dpdHeight;
        private DependencyPropertyDescriptor dpdWidth;
        private DependencyPropertyDescriptor dpdContent;
        private DependencyPropertyDescriptor dpdName;
        private DependencyPropertyDescriptor dpdBackground;
        private DependencyPropertyDescriptor dpdForeground;
        private DependencyPropertyDescriptor dpdFontWeight;
        #endregion

        #region Properties

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Button Component {
            get { return component; } 
            set { component = value; }
        }
        public double Width
        {
            get { return (double) component.GetValue(Button.WidthProperty); }
            set { component.SetValue(Button.WidthProperty, value); }
        }

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

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

        public string Name
        {
            get { return component.GetValue(Button.NameProperty).ToString(); }
            set { component.SetValue(Button.NameProperty, value); }
        }

        public SolidColorBrush Background
        {
            get { return component.GetValue(Button.BackgroundProperty) as SolidColorBrush; }
            set { component.SetValue(Button.BackgroundProperty, value); }
        }

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

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

        #endregion

        public ButtonProxy(Button component)
        {
            if (component == null) throw new ArgumentNullException("component");
            this.component = component;

            CaptureComponent(this.component);
        }

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

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

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

            dpdName = DependencyPropertyDescriptor.FromProperty(Button.NameProperty, typeof(Button));
            dpdName.AddValueChanged(button, OnNameChanged);

            dpdBackground = DependencyPropertyDescriptor.FromProperty(Button.BackgroundProperty, typeof (Button));
            dpdBackground.AddValueChanged(button, OnBackgroundChanged);

            dpdForeground = DependencyPropertyDescriptor.FromProperty(Button.ForegroundProperty, typeof (Button));
            dpdForeground.AddValueChanged(button, OnForegroundChanged);

            dpdFontWeight = DependencyPropertyDescriptor.FromProperty(Button.FontWeightProperty, typeof(Button));
            dpdFontWeight.AddValueChanged(button, OnFontWeightChanged);
        }

        private void ReleaseComponent(Button 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 (dpdName != null)
            {
                dpdName.RemoveValueChanged(button, OnNameChanged);
                dpdName = null;
            }

            if (dpdBackground != null)
            {
                dpdBackground.RemoveValueChanged(button, OnBackgroundChanged);
                dpdBackground = null;
            }

            if (dpdForeground != null)
            {
                dpdForeground.RemoveValueChanged(button, OnForegroundChanged);
                dpdForeground = 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)
        {
            //OnPropertyChanged("Text");
            NotifyPropertyChanged(x => Content);
        }

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

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

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

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

        #region IDisposable Members

        private bool disposed = false;

        ~ButtonProxy()
        {
            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