■ TextBox 엘리먼트의 ContextMenu 속성을 사용해 커스텀 컨텍스트 메뉴를 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <TextBox Name="textBox" Margin="10" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap"> <TextBox.ContextMenu> <ContextMenu Name="contextMenu"> <MenuItem Name="cutMenuItem" Header="Cut" /> <MenuItem Name="copyMenuItem" Header="Copy" /> <MenuItem Name="pasteMenuItem" Header="Paste" /> <Separator/> <MenuItem Name="selectAllMenuItem" Header="Select All" /> <MenuItem Name="selectLineMenuItem" Header="Select Current Line" /> <Separator /> <MenuItem Name="undoMenuItem" Header="Undo Last Action" /> <MenuItem Name="redoMenuItem" Header="Redo Last Action" /> <Separator /> <MenuItem Name="clearMenuItem" Header="Clear All Text" /> </ContextMenu> </TextBox.ContextMenu> This TextBox uses a simple custom context menu. The context menu can be disabled by checking the CheckBox above, which simply sets the TextBox.ContextMenu property to null. </TextBox> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.contextMenu.Opened += contextMenu_Opened; this.cutMenuItem.Click += cutMenuItem_Click; this.copyMenuItem.Click += copyMenuItem_Click; this.pasteMenuItem.Click += pasteMenuItem_Click; this.selectAllMenuItem.Click += selectAllMenuItem_Click; this.selectLineMenuItem.Click += selectLineMenuItem_Click; this.undoMenuItem.Click += undoMenuItem_Click; this.redoMenuItem.Click += redoMenuItem_Click; this.clearMenuItem.Click += clearMenuItem_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 컨텍스트 메뉴 오픈시 처리하기 - contextMenu_Opened(sender, e) /// <summary> /// 컨텍스트 메뉴 오픈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void contextMenu_Opened(object sender, RoutedEventArgs e) { if(this.textBox.SelectedText == string.Empty) { this.copyMenuItem.IsEnabled = this.cutMenuItem.IsEnabled = false; } else { this.copyMenuItem.IsEnabled = this.cutMenuItem.IsEnabled = true; } if(Clipboard.ContainsText()) { this.pasteMenuItem.IsEnabled = true; } else { this.pasteMenuItem.IsEnabled = false; } } #endregion #region Cut 메뉴 항목 클릭시 처리하기 - cutMenuItem_Click(sender, e) /// <summary> /// Cut 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cutMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Cut(); } #endregion #region Copy 메뉴 항목 클릭시 처리하기 - copyMenuItem_Click(sender, e) /// <summary> /// Copy 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void copyMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Copy(); } #endregion #region Paste 메뉴 항목 클릭시 처리하기 - pasteMenuItem_Click(sender, e) /// <summary> /// Paste 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pasteMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Paste(); } #endregion #region Select All 메뉴 항목 클릭시 처리하기 - selectAllMenuItem_Click(sender, e) /// <summary> /// Select All 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectAllMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.SelectAll(); } #endregion #region Select Line 메뉴 항목 클릭시 처리하기 - selectLineMenuItem_Click(sender, e) /// <summary> /// Select Line 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void selectLineMenuItem_Click(object sender, RoutedEventArgs e) { int lineIndex = this.textBox.GetLineIndexFromCharacterIndex(this.textBox.CaretIndex); int characterIndex = this.textBox.GetCharacterIndexFromLineIndex(lineIndex); int lineLength = this.textBox.GetLineLength(lineIndex); this.textBox.Select(characterIndex, lineLength); } #endregion #region Undo 메뉴 항목 클릭시 처리하기 - undoMenuItem_Click(sender, e) /// <summary> /// Undo 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void undoMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Undo(); } #endregion #region Redo 메뉴 항목 클릭시 처리하기 - redoMenuItem_Click(sender, e) /// <summary> /// Redo 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void redoMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Redo(); } #endregion #region Clear 메뉴 항목 클릭시 처리하기 - clearMenuItem_Click(sender, e) /// <summary> /// Clear 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void clearMenuItem_Click(object sender, RoutedEventArgs e) { this.textBox.Clear(); } #endregion } } |