Click here to Skip to main content
15,886,518 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.3K   3.4K   108  
Form designer with editable display properties and bindings. Generates XAML forms.
using System;
using System.IO;
using System.Windows.Input;
using Microsoft.Win32;
using SimpleMvvmToolkit;

namespace WPFFormDesigner.ViewModels
{
    public class MainWindowViewModel : ViewModelBase<MainWindowViewModel>
    {
        private object _SelectedObject;

        #region Constructor

        public MainWindowViewModel()
        {
            SubscribeToMessageBus();
            InitCommands();
            InitViewModels();
        }

        #endregion

        #region Public Properties

        #region Commands

        public ICommand SaveFormCommand { get; set; }
        public ICommand NewFormCommand { get; set; }
        public ICommand OpenFormFromFileCommand { get; set; }

        #endregion

        private static MainWindowViewModel _Instance;
        private FormDesignerSurfaceViewModel _FormDesignerSurfaceVM;

        private PropertiesViewModel _PropertiesVM;

        public FormDesignerSurfaceViewModel FormDesignerSurfaceVM
        {
            get { return _FormDesignerSurfaceVM; }
            set
            {
                _FormDesignerSurfaceVM = value;
                NotifyPropertyChanged(m => m.FormDesignerSurfaceVM);
            }
        }

        public PropertiesViewModel PropertiesVM
        {
            get { return _PropertiesVM; }
            set
            {
                _PropertiesVM = value;
                NotifyPropertyChanged(m => m.PropertiesVM);
            }
        }

        /// <summary>
        /// Property to hold the currently selected object to be displayed in properties
        /// </summary>
        public object SelectedObject
        {
            get { return _SelectedObject; }
            set
            {
                _SelectedObject = value;
                NotifyPropertyChanged(m => SelectedObject);
                //Notify to subscribers that Selected Object has changed
                Notify(SelectedObjectChangedNotice, new NotificationEventArgs<bool>("SelectedObject changed", true));
            }
        }

        public static MainWindowViewModel Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new MainWindowViewModel();
                }
                return _Instance;
            }
            set { _Instance = value; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initialize the ViewModels
        /// </summary>
        private void InitViewModels()
        {
            PropertiesVM = new PropertiesViewModel();
            FormDesignerSurfaceVM = new FormDesignerSurfaceViewModel();
        }

        /// <summary>
        /// Subscribes different type of messages to Message bus so ViewModels can send and 
        /// receive information upon request.
        /// </summary>
        private void SubscribeToMessageBus()
        {
            RegisterToReceiveMessages<object>(MessageTokens.PropertiesObjectSelected,
                                              OnPropertiesObjectSelectedRequested);
        }

        private void OnPropertiesObjectSelectedRequested(object sender, NotificationEventArgs<object> e)
        {
            if (e.Data != null)
                SetSelectedObject(e.Data);
        }

        private void SetSelectedObject(object obj)
        {
            SelectedObject = obj;
        }

        private void InitCommands()
        {
            SaveFormCommand = new RelayCommand(OnSaveForm, CanSaveForm);
            OpenFormFromFileCommand = new RelayCommand(OpenFormFromFile);
            NewFormCommand = new RelayCommand(OnNewFormCommand);
        }

        private void OnNewFormCommand()
        {
            var fdsvm = new FormDesignerSurfaceViewModel();
            fdsvm.IsNew = true;
            FormDesignerSurfaceVM = fdsvm;
        }

        private bool CanSaveForm()
        {
            return true;
        }

        private void OnSaveForm()
        {
            if (FormDesignerSurfaceVM != null)
            {
                FormDesignerSurfaceVM.SaveAsCommand.Execute(null);
            }
        }

        public void OpenFormFromFile()
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "XML File|*.xml";
            dlg.Title = "Open XML File";
            if (dlg.ShowDialog().GetValueOrDefault())
            {
                string xml = File.ReadAllText(dlg.FileName);
                if (!string.IsNullOrEmpty(xml))
                {
                    var fdsvm = new FormDesignerSurfaceViewModel();
                    fdsvm.XamlForm = xml;
                    FormDesignerSurfaceVM = fdsvm;
                }
            }
        }

        #endregion

        #region Events

        public event EventHandler<NotificationEventArgs<bool>> SelectedObjectChangedNotice;

        #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