Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / XAML

A dynamic Rehosted Workflow Designer for WF 4

Rate me:
Please Sign up or sign in to vote.
4.85/5 (26 votes)
29 Jul 2012CPOL10 min read 112.1K   11.8K   78  
This article presents a framework allowing you to integrate the workflow designer more easily in your own applications
using System;
using System.Activities.Core.Presentation;
using System.Activities.Presentation;
using System.ComponentModel;
using System.IO;
using System.Windows.Input;
using System.Windows.Threading;
using RehostedDesigner.Designer.ViewModels.Contracts;
using RehostedDesigner.LoadErrorHandling;

namespace RehostedDesigner.Designer.ViewModels
{
    public class StandardDesignerViewModel : INotifyPropertyChanged, IDesignerViewModel, ILoadErrorDesignerViewModel
    {
        private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

        public StandardDesignerViewModel()
        {
            this.CurrentSurface = new EmptyDesignerSurface();
        }

        public object CurrentSurface
        {
            get;
            private set;
        }

        public event Action SurfaceChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        public void ReloadError(string xaml)
        {
            this.CurrentSurface = new LoadErrorDesignerSurface(xaml);
            this._dispatcher.Invoke(new Action(this.OnSurfaceChanged));
        }

        public void ReloadDesigner(object root)
        {
            WorkflowDesigner designer = null;

            this._dispatcher.Invoke(new Action(() => designer = ReloadDesignerSTA(root)));
            this.CurrentSurface = new StandardDesignerSurface(designer);
            this._dispatcher.Invoke(new Action(this.OnSurfaceChanged));
        }

        private WorkflowDesigner ReloadDesignerSTA(object root)
        {
            var result = new WorkflowDesigner();
            new DesignerMetadata().Register();
            result.Load(root);

            return result;
        }

        private void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        private void OnSurfaceChanged()
        {
            this.OnPropertyChanged("CurrentSurface");

            if (this.SurfaceChanged != null)
            {
                this.SurfaceChanged();
            }
        }
    }
}

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
Student
Germany Germany
I´m a student and hobby programmer from Germany.

Comments and Discussions