Click here to Skip to main content
15,887,267 members
Articles / Web Development / HTML

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.5K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
// This file is part of the Lobster.MVVM library
// Copyright: Andreas Raczek
// This file is published under the The Code Project Open License (CPOL) 
// See the file "CPOL.html" for the full license governing this code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Lobster.MVVM
{
    public interface IWindowHandle 
    {
        void Show();

        void ShowModal();

        void Close();        
    }

    public class WindowHandle : IWindowHandle
    {
        public UIElementVM ViewModel { get; private set; }
        public Window Window { get; private set; }

        public WindowHandle(Window w, UIElementVM vm) {
            Window = w;
            ViewModel = vm;
        }

        public void Show()
        {
            Window.Show();
        }

        public void ShowModal()
        {
            Window.ShowDialog();
        }

        public void Close()
        {
            Window.Close();
        }
    }

    public interface IWindowServices
    {
        void Add(string name, Type t);
        bool Remove(string name);

        // IWindowHandle Create(string name, UIElementVM viewModel);
        IWindowHandle Create(string name, WindowVM viewModel, WindowVM parentViewModel);
        void CloseAllWindows();

        IWindowHandle GetWindowHandle(WindowVM viewModel);
    }

    public class WindowServices : IWindowServices
    {

        private Dictionary<string, Type> dialogTypes = new Dictionary<string,Type>();
        private List<WindowHandle> windows = new List<WindowHandle>();

        private int currentID = 0;
        private int getNewID() {
            return currentID++;
        }

        public void Add(string name, Type t)
        {
            dialogTypes.Add(name, t);
        }

        public bool Remove(string name)
        {
            return dialogTypes.Remove(name);
        }

        private IWindowHandle _Create(string name, UIElementVM viewModel, UIElementVM parentViewModel) 
        {
            if (!dialogTypes.ContainsKey(name)) throw new ArgumentException();

            Type t = dialogTypes[name];
            Window w = (Window)Activator.CreateInstance(t);

            if (null != parentViewModel)
            {
                var pwh = windows.Where((h) => h.ViewModel == parentViewModel).First();
                if (null != pwh)
                {
                    w.Owner = pwh.Window;
                }
            }

            w.DataContext = viewModel;
            var newWindow = new WindowHandle(w, viewModel);
            windows.Add(newWindow);
            return newWindow;
        }

        public IWindowHandle Create(string name, WindowVM viewModel, WindowVM parentViewModel) 
        {
            IWindowHandle dlgHandle = _Create(name, (UIElementVM) viewModel, parentViewModel);

            viewModel.RequestClose += (o, p) =>
            {
                dlgHandle.Close();
            };

            return dlgHandle;
        }

        public void CloseAllWindows()
        {
            foreach (var w in windows) w.Close();
        }

        public IWindowHandle GetWindowHandle(WindowVM viewModel)
        {
            foreach (var w in windows)
            {
                if (w.ViewModel == viewModel) return w;
            }
            return null;
        }
    }

}

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

Comments and Discussions