using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 키 제스처 딕셔너리
/// </summary>
Dictionary<KeyGesture, MenuItem> keyGestureDictionary = new Dictionary<KeyGesture, MenuItem>();
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.editMenuItem.SubmenuOpened += editMenuItem_SubmenuOpened;
this.cutMenuItem.Click += cutMenuItem_Click;
this.copyMenuItem.Click += copyMenuItem_Click;
this.pasteMenuItem.Click += pasteMenuItem_Click;
this.deleteMenuItem.Click += deleteMenuItem_Click;
this.keyGestureDictionary.Add(new KeyGesture(Key.X, ModifierKeys.Control), this.cutMenuItem );
this.keyGestureDictionary.Add(new KeyGesture(Key.C, ModifierKeys.Control), this.copyMenuItem );
this.keyGestureDictionary.Add(new KeyGesture(Key.V, ModifierKeys.Control), this.pasteMenuItem );
this.keyGestureDictionary.Add(new KeyGesture(Key.Delete ), this.deleteMenuItem);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 키 DOWN 사전 처리하기 - OnPreviewKeyDown(e)
/// <summary>
/// 키 DOWN 사전 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
foreach(KeyGesture keyGesture in this.keyGestureDictionary.Keys)
{
if(keyGesture.Matches(null, e))
{
e.Handled = true;
this.keyGestureDictionary[keyGesture].RaiseEvent
(
new RoutedEventArgs
(
MenuItem.ClickEvent,
this.keyGestureDictionary[keyGesture]
)
);
}
}
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 편집 메뉴 항목 하위 메뉴 오픈시 처리하기 - editMenuItem_SubmenuOpened(sender, e)
/// <summary>
/// 편집 메뉴 항목 하위 메뉴 오픈시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void editMenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
bool isEnabled = (this.textBlock.Text != null && this.textBlock.Text.Length > 0);
this.cutMenuItem.IsEnabled = isEnabled;
this.copyMenuItem.IsEnabled = isEnabled;
this.pasteMenuItem.IsEnabled = Clipboard.ContainsText();
this.deleteMenuItem.IsEnabled = isEnabled;
}
#endregion
#region 잘라내기 메뉴 항목 클릭시 처리하기 - cutMenuItem_Click(sender, e)
/// <summary>
/// 잘라내기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void cutMenuItem_Click(object sender, RoutedEventArgs e)
{
copyMenuItem_Click(sender, e);
deleteMenuItem_Click(sender, e);
}
#endregion
#region 복사하기 메뉴 항목 클릭시 처리하기 - copyMenuItem_Click(sender, e)
/// <summary>
/// 복사하기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void copyMenuItem_Click(object sender, RoutedEventArgs e)
{
if(this.textBlock.Text != null && this.textBlock.Text.Length > 0)
{
Clipboard.SetText(this.textBlock.Text);
}
}
#endregion
#region 붙여넣기 메뉴 항목 클릭시 처리하기 - pasteMenuItem_Click(sender, e)
/// <summary>
/// 붙여넣기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void pasteMenuItem_Click(object sender, RoutedEventArgs e)
{
if(Clipboard.ContainsText())
{
this.textBlock.Text = Clipboard.GetText();
}
}
#endregion
#region 삭제하기 메뉴 항목 클릭시 처리하기 - deleteMenuItem_Click(sender, e)
/// <summary>
/// 삭제하기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
protected void deleteMenuItem_Click(object sender, RoutedEventArgs e)
{
this.textBlock.Text = null;
}
#endregion
}
}