■ RoutedUICommand 클래스 : 메뉴 실행하기

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

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

 

/// <summary>

/// 메인 윈도우

/// </summary>

public class MainWindow : Window

{

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

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

 

    #region Field

 

    /// <summary>

    /// 텍스트 블럭

    /// </summary>

    private TextBlock textBlock;

 

    #endregion

 

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

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

 

    #region 생성자 - MainWindow()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public MainWindow()

    {

        Title  = "메뉴 명령 사용하기";

        Width  = 600;

        Height = 450;

 

 

        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            = 32;

        this.textBlock.TextWrapping        = TextWrapping.Wrap;

 

        dockPanel.Children.Add(this.textBlock);

 

 

        MenuItem editMenuItem = new MenuItem();

 

        editMenuItem.Header = "_Edit";

 

        menu.Items.Add(editMenuItem);

 

 

        MenuItem cutMenuItem = new MenuItem();

 

        cutMenuItem.Header  = "Cu_t";

        cutMenuItem.Command = ApplicationCommands.Cut;

 

        editMenuItem.Items.Add(cutMenuItem);

 

 

        MenuItem copyMenuItem = new MenuItem();

 

        copyMenuItem.Header  = "_Copy";

        copyMenuItem.Command = ApplicationCommands.Copy;

 

        editMenuItem.Items.Add(copyMenuItem);

 

 

        MenuItem pasteMenuItem = new MenuItem();

 

        pasteMenuItem.Header  = "_Paste";

        pasteMenuItem.Command = ApplicationCommands.Paste;

 

        editMenuItem.Items.Add(pasteMenuItem);

 

 

        MenuItem deleteMenuItem = new MenuItem();

 

        deleteMenuItem.Header  = "_Delete";

        deleteMenuItem.Command = ApplicationCommands.Delete;

 

        editMenuItem.Items.Add(deleteMenuItem);

 

 

        editMenuItem.Items.Add(new Separator());

 

 

        RoutedUICommand restoreRoutedUICommand = GetRestoreRoutedUICommand();

 

 

        MenuItem restoreMenuItem = new MenuItem();

 

        restoreMenuItem.Header  = "_Restore";

        restoreMenuItem.Command = restoreRoutedUICommand;

 

        editMenuItem.Items.Add(restoreMenuItem);

 

 

        CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut   , cut_Executed    , cut_CanExecute  ));

        CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy  , copy_Executed   , cut_CanExecute  ));

        CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste , paste_Executed  , paste_CanExecute));

        CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, delete_Executed , cut_CanExecute  ));

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

    }

 

    #endregion

 

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

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

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

 

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

 

    /// <summary>

    /// 프로그램 시작하기

    /// </summary>

    [STAThread]

    public static void Main()

    {

        Application application = new Application();

 

        application.Run(new MainWindow());

    }

 

    #endregion

 

    ////////////////////////////////////////////////////////////////////////////////////////// Instance

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

 

    #region CUT 실행 가능 여부 조사시 처리하기 - cut_CanExecute(sender, e)

 

    /// <summary>

    /// CUT 실행 가능 여부 조사시 처리하기

    /// </summary>

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

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

    private void cut_CanExecute(object sender, CanExecuteRoutedEventArgs e)

    {

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

    }

 

    #endregion

 

    #region CUT 실행시 처리하기 - cut_Executed(sender, e)

 

    /// <summary>

    /// CUT 실행시 처리하기

    /// </summary>

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

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

    private void cut_Executed(object sender, ExecutedRoutedEventArgs e)

    {

        ApplicationCommands.Copy.Execute(null, this);

 

        ApplicationCommands.Delete.Execute(null, this);

    }

 

    #endregion

 

    #region COPY 실행시 처리하기 - copy_Executed(sender, e)

 

    /// <summary>

    /// COPY 실행시 처리하기

    /// </summary>

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

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

    private void copy_Executed(object sender, ExecutedRoutedEventArgs e)

    {

        Clipboard.SetText(this.textBlock.Text);

    }

 

    #endregion

 

    #region PASTE 실행 가능 여부 조사시 처리하기 - paste_CanExecute(sender, e)

 

    /// <summary>

    /// PASTE 실행 가능 여부 조사시 처리하기

    /// </summary>

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

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

    private void paste_CanExecute(object sender, CanExecuteRoutedEventArgs e)

    {

        e.CanExecute = Clipboard.ContainsText();

    }

 

    #endregion

 

    #region PASTE 실행시 처리하기 - paste_Executed(sender, e)

 

    /// <summary>

    /// PASTE 실행시 처리하기

    /// </summary>

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

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

    private void paste_Executed(object sender, ExecutedRoutedEventArgs e)

    {

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

    }

 

    #endregion

 

    #region DELETE 실행시 처리하기 - delete_Executed(sender, e)

 

    /// <summary>

    /// DELETE 실행시 처리하기

    /// </summary>

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

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

    private void delete_Executed(object sender, ExecutedRoutedEventArgs e)

    {

        this.textBlock.Text = null;

    }

 

    #endregion

 

    #region RESTORE 실행시 처리하기 - restore_Executed(sender, e)

 

    /// <summary>

    /// RESTORE 실행시 처리하기

    /// </summary>

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

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

    private void restore_Executed(object sender, ExecutedRoutedEventArgs e)

    {

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

    }

 

    #endregion

 

 

    #region RESTORE 라우팅 UI 명령 구하기 - GetRestoreRoutedUICommand()

 

    /// <summary>

    /// RESTORE 라우팅 UI 명령 구하기

    /// </summary>

    /// <returns>RESTORE 라우팅 UI 명령</returns>

    private RoutedUICommand GetRestoreRoutedUICommand()

    {

        InputGestureCollection inputGestureCollection = new InputGestureCollection();

 

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

 

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

 

        return restoreRoutedUICommand;

    }

 

    #endregion    

}

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요