Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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

, 5 Jan 2012
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.
objectpresentation_demo.zip
ObjectPresentation_Demo
Client
ObjectPresentation.dll
ObjectPresentation.Examples.Client.exe
ObjectPresentation.Examples.Client.pdb
ObjectPresentation.Examples.Contracts.dll
ObjectPresentation.Examples.Contracts.pdb
ObjectPresentation.pdb
Server
ObjectPresentation.Examples.Contracts.dll
ObjectPresentation.Examples.Contracts.pdb
ObjectPresentation.Examples.Server.exe
ObjectPresentation.Examples.Server.pdb
ObjectPresentation.Examples.Service.dll
ObjectPresentation.Examples.Service.pdb
WcfLogPrints.dll
objectpresentation_src.zip
ObjectPresentation_src
ObjectPresentation
bin
Debug
Release
obj
Debug
Properties
Settings.settings
Styles
Themes
ObjectPresentation.Examples.Client
bin
Debug
Release
obj
x86
Debug
Properties
Settings.settings
ObjectPresentation.Examples.Contracts
bin
Debug
Release
obj
Debug
Properties
ObjectPresentation.Examples.Server
bin
Debug
Release
obj
x86
Debug
Properties
ObjectPresentation.Examples.Service
bin
Debug
obj
Debug
Properties
WcfLogPrints.dll
ObjectPresentation.suo
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)

Share

About the Author

Shmuel Zang
Software Developer
Israel Israel
No Biography provided
Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 5 Jan 2012
Article Copyright 2012 by Shmuel Zang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid