첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ CommandBinding 클래스 : 메뉴 명령 사용하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.cs

 

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메뉴 명령하기

    /// </summary>

    public class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 텍스트 블럭

        /// </summary>

        private TextBlock textBlock;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800;

            Height     = 600;

            Title      = "CommandBinding 클래스 : 메뉴 명령 사용하기";

            FontFamily = new FontFamily("나눔고딕코딩");

            FontSize   = 16;

 

            DockPanel dockPanel = new DockPanel();

 

            Content = dockPanel;

 

            Menu menu = new Menu();

 

            dockPanel.Children.Add(menu);

 

            DockPanel.SetDock(menu, Dock.Top);

 

            this.textBlock = new TextBlock();

 

            this.textBlock.Text                = "샘플 클립보드 텍스트";

            this.textBlock.HorizontalAlignment = HorizontalAlignment.Center;

            this.textBlock.VerticalAlignment   = VerticalAlignment.Center;

            this.textBlock.FontSize            = 32d;

            this.textBlock.TextWrapping        = TextWrapping.Wrap;

 

            dockPanel.Children.Add(this.textBlock);

 

            #region Edit 메뉴

 

            MenuItem editMenuitem = new MenuItem();

 

            editMenuitem.Header = "_Edit";

 

            menu.Items.Add(editMenuitem);

 

            #endregion

                #region Cut 메뉴

 

                MenuItem cutMenuItem = new MenuItem();

 

                cutMenuItem.Header  = "Cu_t";

                cutMenuItem.Command = ApplicationCommands.Cut;

 

                editMenuitem.Items.Add(cutMenuItem);

 

                #endregion

                #region Copy 메뉴

 

                MenuItem copyMenuItem = new MenuItem();

 

                copyMenuItem.Header  = "_Copy";

                copyMenuItem.Command = ApplicationCommands.Copy;

 

                editMenuitem.Items.Add(copyMenuItem);

 

                #endregion

                #region Paste 메뉴

 

                MenuItem pasteMenuItem = new MenuItem();

 

                pasteMenuItem.Header  = "_Paste";

                pasteMenuItem.Command = ApplicationCommands.Paste;

 

                editMenuitem.Items.Add(pasteMenuItem);

 

                #endregion

                #region Delete 메뉴

 

                MenuItem deleteMenuItem = new MenuItem();

 

                deleteMenuItem.Header  = "_Delete";

                deleteMenuItem.Command = ApplicationCommands.Delete;

 

                editMenuitem.Items.Add(deleteMenuItem);

 

                #endregion

                #region Restore 메뉴

 

                InputGestureCollection inputGestureCollection = new InputGestureCollection();

 

                inputGestureCollection.Add(new KeyGesture(Key.R, ModifierKeys.Control));

 

                RoutedUICommand restoreRoutedUICommand = new RoutedUICommand("_Restore", "Restore", GetType(), inputGestureCollection);

 

                MenuItem restoreMenuItem = new MenuItem();

 

                restoreMenuItem.Header  = "_Restore";

                restoreMenuItem.Command = restoreRoutedUICommand;

 

                editMenuitem.Items.Add(restoreMenuItem);

 

                #endregion

 

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut   , CutExecuted   , CutCanExecute  ));

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy  , CopyExecuted  , CutCanExecute  ));

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste , PasteExecuted , PasteCanExecute));

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, DeleteExecuted, CutCanExecute  ));

            CommandBindings.Add(new CommandBinding(restoreRoutedUICommand    , RestoreExecuted                ));

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Intance

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Cut 실행 가능 여부 조사하기 - CutCanExecute(sender, e)

 

        /// <summary>

        /// Cut 실행 가능 여부 조사하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void CutCanExecute(object sender, CanExecuteRoutedEventArgs e)

        {

            e.CanExecute = this.textBlock.Text != null && this.textBlock.Text.Length > 0;

        }

 

        #endregion

        #region Cut 실행시 처리하기 - CutExecuted(sender, e)

 

        /// <summary>

        /// Cut 실행시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void CutExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            ApplicationCommands.Copy.Execute(null, this);

 

            ApplicationCommands.Delete.Execute(null, this);

        }

 

        #endregion

        #region Paste 실행 가능 여부 조사하기 - PasteCanExecute(sender, e)

 

        /// <summary>

        /// Paste 실행 가능 여부 조사하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void PasteCanExecute(object sender, CanExecuteRoutedEventArgs e)

        {

            e.CanExecute = Clipboard.ContainsText();

        }

 

        #endregion

        #region Paste 실행시 처리하기 - PasteExecuted(sender, e)

 

        /// <summary>

        /// Paste 실행시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void PasteExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            this.textBlock.Text = Clipboard.GetText();

        }

 

        #endregion

        #region Copy 실행시 처리하기 - CopyExecuted(sender, e)

 

        /// <summary>

        /// Copy 실행시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            Clipboard.SetText(this.textBlock.Text);

        }

 

        #endregion

        #region Delete 실행시 처리하기 - DeleteExecuted(sender, e)

 

        /// <summary>

        /// Delete 실행시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            this.textBlock.Text = null;

        }

 

        #endregion

        #region Restore 실행시 처리하기 - RestoreExecuted(sender, e)

 

        /// <summary>

        /// Restore 실행시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void RestoreExecuted(object sender, ExecutedRoutedEventArgs e)

        {

            this.textBlock.Text = "샘플 클립보드 텍스트";

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요