첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<dx:DXWindow x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    mc:Ignorable="d"
    Width="800"
    Height="600"
    FontFamily="나눔고딕코딩"
    FontSize="16"
    Title="DXTabControl 클래스 : 탭 MDI 사용하기">
    <Grid>
        <dxb:BarManager x:Name="barManager">
            <dxb:BarManager.Bars>
                <dxb:Bar x:Name="mainMenuBar"
                    IsMainMenu="True"
                    Caption="Main Menu">
                    <dxb:Bar.DockInfo>
                        <dxb:BarDockInfo ContainerType="Top" />
                    </dxb:Bar.DockInfo>
                </dxb:Bar>
                <dxb:Bar x:Name="statusBar"
                    IsStatusBar="True"
                    Caption="Status Bar">
                    <dxb:Bar.DockInfo>
                        <dxb:BarDockInfo ContainerType="Bottom" />
                    </dxb:Bar.DockInfo>
                    <dxb:BarStaticItem Content="rover" Alignment="Near" />
                    <dxb:BarStaticItem x:Name="loginBarStaticItem" Alignment="Far" />
                </dxb:Bar>
            </dxb:BarManager.Bars>
            <dx:DXTabControl x:Name="tabControl"
                Margin="5"
                Visibility="Collapsed">
                <dx:DXTabControl.View>
                    <dx:TabControlScrollView
                        HeaderLocation="Bottom"
                        HideButtonShowMode="InAllTabs" />
                </dx:DXTabControl.View>
            </dx:DXTabControl>
        </dxb:BarManager>
    </Grid>
</dx:DXWindow>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Remoting;
using System.Windows;
using System.IO;

using DevExpress.Xpf.Core;
using DevExpress.Xpf.Bars;

using TestLibrary;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : DXWindow
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 모듈 정보 딕셔너리
        /// </summary>
        private Dictionary<string, ModuleInfo> moduleInfoDictionary = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.moduleInfoDictionary = GetModuleInfoDictionary();

            ObservableCollection<MenuInfo> guestCollection = GetMenuInfoCollection();

            MakeMenu(this.mainMenuBar, guestCollection);

            this.tabControl.TabHidden += tabControl_TabHidden;

            this.loginBarStaticItem.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 메뉴 아이템 아이템 클릭시 처리하기 - menuItem_ItemClick(sender, e)

        /// <summary>
        /// 메뉴 아이템 아이템 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void menuItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            BarButtonItem barButtonItem = sender as BarButtonItem;

            MenuInfo menuInfo = barButtonItem.Tag as MenuInfo;

            if(string.IsNullOrEmpty(menuInfo.ModuleID))
            {
                return;
            }

            if(this.moduleInfoDictionary.ContainsKey(menuInfo.ModuleID))
            {
                ModuleInfo moduleInfo = this.moduleInfoDictionary[menuInfo.ModuleID];

                foreach(object item in this.tabControl.Items)
                {
                    DXTabItem tabItem = item as DXTabItem;

                    if(tabItem == null)
                    {
                        continue;
                    }

                    if((tabItem.Tag as MenuInfo).ID.Equals(menuInfo.ID))
                    {
                        this.tabControl.SelectedItem = tabItem;

                        return;
                    }
                }

                AddTabItem(this.tabControl, menuInfo, moduleInfo);
            }
        }

        #endregion
        #region 탭 컨트롤 탭 숨기는 경우 처리하기 - tabControl_TabHidden(sender, e)

        /// <summary>
        /// 탭 컨트롤 탭 숨기는 경우 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void tabControl_TabHidden(object sender, TabControlTabHiddenEventArgs e)
        {
            DXTabControl tabControl = sender as DXTabControl;

            tabControl.Items.Remove(e.Item);

            if(tabControl.Items.Count == 0)
            {
                tabControl.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        #region 모듈 정보 딕셔너리 구하기 - GetModuleInfoDictionary()

        /// <summary>
        /// 모듈 정보 딕셔너리 구하기
        /// </summary>
        /// <returns>모듈 정보 딕셔너리</returns>
        public Dictionary<string, ModuleInfo> GetModuleInfoDictionary()
        {
            Dictionary<string, ModuleInfo> dictionary = new Dictionary<string, ModuleInfo>();

            dictionary.Add("TEST1", new ModuleInfo() { ID = "TEST1", Name = "테스트1", AssemblyFilePath = "TestLibrary.dll", ModuleType = "TestLibrary.RedControl"  });
            dictionary.Add("TEST2", new ModuleInfo() { ID = "TEST2", Name = "테스트2", AssemblyFilePath = "TestLibrary.dll", ModuleType = "TestLibrary.BlueControl" });

            return dictionary;
        }

        #endregion
        #region 메뉴 정보 컬렉션 구하기 - GetMenuInfoCollection()

        /// <summary>
        /// 메뉴 정보 컬렉션 구하기
        /// </summary>
        /// <returns>손님 컬렉션</returns>
        public ObservableCollection<MenuInfo> GetMenuInfoCollection()
        {
            ObservableCollection<MenuInfo> collection = new ObservableCollection<MenuInfo>();

            MenuInfo fileMenuInfo = new MenuInfo();

            fileMenuInfo.ID                      = "FILE";
            fileMenuInfo.Name                    = "파일";
            fileMenuInfo.ModuleID                = null;
            fileMenuInfo.Shortcut                = "F";
            fileMenuInfo.Icon                    = null;
            fileMenuInfo.Parameter1              = null;
            fileMenuInfo.Parameter1              = null;
            fileMenuInfo.Parameter2              = null;
            fileMenuInfo.Parameter3              = null;
            fileMenuInfo.Parameter4              = null;
            fileMenuInfo.ChildMenuInfoCollection = new ObservableCollection<MenuInfo>();

            collection.Add(fileMenuInfo);

            MenuInfo logInMenuInfo = new MenuInfo();

            logInMenuInfo.ID                      = "MENU1";
            logInMenuInfo.Name                    = "화면 #1";
            logInMenuInfo.ModuleID                = "TEST1";
            logInMenuInfo.Shortcut                = "1";
            logInMenuInfo.Icon                    = null;
            logInMenuInfo.Parameter1              = null;
            logInMenuInfo.Parameter1              = null;
            logInMenuInfo.Parameter2              = null;
            logInMenuInfo.Parameter3              = null;
            logInMenuInfo.Parameter4              = null;
            logInMenuInfo.ChildMenuInfoCollection = null;

            fileMenuInfo.ChildMenuInfoCollection.Add(logInMenuInfo);

            MenuInfo exitMenuInfo = new MenuInfo();

            exitMenuInfo.ID                      = "MENU2";
            exitMenuInfo.Name                    = "화면 #2";
            exitMenuInfo.ModuleID                = "TEST2";
            exitMenuInfo.Shortcut                = "2";
            exitMenuInfo.Icon                    = null;
            exitMenuInfo.Parameter1              = null;
            exitMenuInfo.Parameter1              = null;
            exitMenuInfo.Parameter2              = null;
            exitMenuInfo.Parameter3              = null;
            exitMenuInfo.Parameter4              = null;
            exitMenuInfo.ChildMenuInfoCollection = null;

            fileMenuInfo.ChildMenuInfoCollection.Add(exitMenuInfo);

            return collection;
        }

        #endregion

        #region 객체 구하기 - GetObject(assemblyFilePath, targetType)

        /// <summary>
        /// 객체 구하기
        /// </summary>
        /// <param name="assemblyFilePath">어셈블리 파일명</param>
        /// <param name="targetType">타겟 타입</param>
        /// <returns>object</returns>
        public object GetObject(string assemblyFilePath, string targetType)
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyFilePath);
            
            ObjectHandle objectHandle = Activator.CreateInstanceFrom(filePath, targetType);

            return objectHandle.Unwrap();
        }

        #endregion

        #region 메뉴 만들기 - MakeMenu(bar, menuInfoCollection)

        /// <summary>
        /// 메뉴 만들기
        /// </summary>
        /// <param name="bar">바</param>
        /// <param name="menuInfoCollection">메뉴 정보 컬렉션</param>
        private void MakeMenu(Bar bar, ObservableCollection<MenuInfo> menuInfoCollection)
        {
            bar.Items.Clear();

            foreach(MenuInfo menuInfo in menuInfoCollection)
            {
                MakeMenu(bar.Items, menuInfo);
            }
        }

        #endregion
        #region 메뉴 만들기 - MakeMenu(commonBarItemCollection, menuInfo)

        /// <summary>
        /// 메뉴 만들기
        /// </summary>
        /// <param name="commonBarItemCollection">공통 바 아이템 컬렉션</param>
        /// <param name="menuInfo">메뉴 정보</param>
        private void MakeMenu(CommonBarItemCollection commonBarItemCollection, MenuInfo menuInfo)
        {
            if(string.IsNullOrEmpty(menuInfo.ModuleID))
            {
                BarSubItem barSubItem = new BarSubItem();

                if(string.IsNullOrEmpty(menuInfo.Shortcut))
                {
                    barSubItem.Content = menuInfo.Name;
                }
                else
                {
                    barSubItem.Content = string.Format("{0}(_{1})", menuInfo.Name, menuInfo.Shortcut);
                }

                barSubItem.Glyph = menuInfo.Icon;
                barSubItem.Tag   = menuInfo;

                commonBarItemCollection.Add(barSubItem);

                foreach(MenuInfo childMenuInfo in menuInfo.ChildMenuInfoCollection)
                {
                    MakeMenu(barSubItem.Items, childMenuInfo);
                }
            }
            else
            {
                BarButtonItem barButtonItem = new BarButtonItem();

                if(string.IsNullOrEmpty(menuInfo.Shortcut))
                {
                    barButtonItem.Content = menuInfo.Name;
                }
                else
                {
                    barButtonItem.Content = string.Format("{0}(_{1})", menuInfo.Name, menuInfo.Shortcut);
                }

                barButtonItem.Glyph = menuInfo.Icon;
                barButtonItem.Tag   = menuInfo;

                barButtonItem.ItemClick += menuItem_ItemClick;

                commonBarItemCollection.Add(barButtonItem);
            }
        }

        #endregion
        #region 탭 아이템 추가하기 - AddTabItem(tabControl, menuInfo, moduleInfo)

        /// <summary>
        /// 탭 아이템 추가하기
        /// </summary>
        /// <param name="tabControl">탭 컨트롤</param>
        /// <param name="menuInfo">메뉴 정보</param>
        /// <param name="moduleInfo">모듈 정보</param>
        private void AddTabItem(DXTabControl tabControl, MenuInfo menuInfo, ModuleInfo moduleInfo)
        {
            tabControl.Visibility = Visibility.Visible;

            DXTabItem tab = new DXTabItem();

            tab.Header  = menuInfo.Name;
            tab.Content = GetObject(moduleInfo.AssemblyFilePath, moduleInfo.ModuleType);
            tab.Tag     = menuInfo;

            tabControl.Items.Add(tab);

            tabControl.SelectedItem = tab;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요