Click here to Skip to main content
15,881,715 members
Articles / Desktop Programming / WPF

ObjectPresenter - How to Generate an Object's Testing GUI from a Given Object

Rate me:
Please Sign up or sign in to vote.
4.94/5 (27 votes)
5 Jan 2012CPOL11 min read 37.2K   924   49  
In this article, I explain step by step, how we can create a WPF custom control that gets an object and, generates a GUI that enables editing that object's properties and invoking that object's methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Threading;
using System.Threading;

namespace ObjectPresentation.Examples.Client
{
    /// <summary>
    /// Interaction logic for MethodPresenterExample.xaml
    /// </summary>
    public partial class MethodPresenterExample : Window
    {
        public MethodPresenterExample()
        {
            InitializeComponent();

            myMethodPresenter.MethodInformation = this.GetType().GetMethod("AddShape");
            myMethodPresenter.ObjectInstance = this;
        }

        public void AddShape(MyShape shape)
        {
            if (shape == null)
            {
                return;
            }

            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new ThreadStart(() =>
                {

                    Shape element = null;

                    if (shape is MyCircle)
                    {
                        MyCircle mc = shape as MyCircle;
                        element = new Ellipse
                        {
                            Width = mc.Diameter,
                            Height = mc.Diameter
                        };
                    }
                    else if (shape is MyRectangle)
                    {
                        MyRectangle mr = shape as MyRectangle;
                        element = new Rectangle
                        {
                            Width = mr.Width,
                            Height = mr.Height,
                        };
                    }

                    if (element != null)
                    {
                        element.Fill = new SolidColorBrush(shape.FillColor);
                        element.Stroke = new SolidColorBrush(shape.StrokeColor);
                        element.StrokeThickness = shape.StrokeThickness;

                        Canvas.SetLeft(element, shape.Left);
                        Canvas.SetTop(element, shape.Top);

                        myCanvas.Children.Add(element);
                    }
                }));
        }
    }
}

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
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions