Click here to Skip to main content
15,885,985 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.
// -------------------------------------------------------------------------------
// 
// This file is part of the FluidKit project: http://www.codeplex.com/fluidkit
// 
// Copyright (c) 2008, The FluidKit community 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
// 
// * Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimer.
// 
// * Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or 
// other materials provided with the distribution.
// 
// * Neither the name of FluidKit nor the names of its contributors may be used to 
// endorse or promote products derived from this software without specific prior 
// written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
// -------------------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using FluidKit.Helpers.DragDrop;
using WPFFormDesigner.WPFControlUtils;
using WPFFormDesigner.XamlSerializer;

namespace WPFFormDesigner.WPFControlUtils.DragDrop
{
	public class DefaultDragSourceAdvisor : IDragSourceAdvisor
	{
		private static DataFormat SupportedFormat = DataFormats.GetDataFormat("FluidKit");
        private XAMLSerializer _XamlSerializer = new XAMLSerializer();

	    public DefaultDragSourceAdvisor()
	    {
            EditorHelper.Register<BindingExpression, BindingConvertor>();
	    }
		#region IDragSourceAdvisor Members

		public DragDropEffects SupportedEffects
		{
			get { return DragDropEffects.Copy | DragDropEffects.Move; }
		}

		public UIElement SourceUI { get; set; }

		public DataObject GetDataObject(UIElement draggedElt)
		{
			//string serializedObject1 = XamlWriter.Save(draggedElt);
            if (draggedElt is ListView)
            {
                draggedElt = UIHelper.FindParent<StackPanel>(draggedElt, true);
            }

            //Handle Custom controls as button. Convert from button to real control
		    Button btn = draggedElt as Button;
            if (btn != null)
            {
                switch (btn.Tag as string)
                {
                    case "ObservableCollectionListView":
                        draggedElt = new ObservableCollectionListView();
                        break;
                }
            }

		    string serializedObject = _XamlSerializer.SerializeControlToXaml(draggedElt as FrameworkElement);
			DataObject data = new DataObject(SupportedFormat.Name, serializedObject);

			return data;
		}

		public void FinishDrag(UIElement draggedElt, DragDropEffects finalEffects)
		{
            //if ((finalEffects & DragDropEffects.Move) == DragDropEffects.Move)
            //{
            //    Panel panel = SourceUI as Panel;
            //    if (panel != null)
            //    {
            //        panel.Children.Remove(draggedElt);
            //    }
            //}
		}

		public bool IsDraggable(UIElement dragElt)
		{
			return (dragElt is Button ||
                    dragElt is TextBox||
                    dragElt is TextBlock ||
                    dragElt is Label ||
                    dragElt is ComboBox ||
                    dragElt is CheckBox ||
                    dragElt is DatePicker ||
                    dragElt is Expander ||
                    dragElt is RadioButton ||
                    dragElt is ObservableCollectionListView);
		}

		public UIElement GetTopContainer()
		{
			return Application.Current.MainWindow.Content as UIElement;
		}

		#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