다음을 통해 공유


방법: TextBox에 사용자 지정 컨텍스트 메뉴 사용

업데이트: 2007년 11월

이 예제에서는 TextBox에 대한 간단한 사용자 지정 컨텍스트 메뉴를 정의하고 구현하는 방법을 보여 줍니다.

예제

다음 XAML(Extensible Application Markup Language) 예제에서는 사용자 지정 컨텍스트 메뉴를 포함하는 TextBox 컨트롤을 정의합니다.

컨텍스트 메뉴는 ContextMenu 요소를 사용하여 정의됩니다. 컨텍스트 메뉴 자체는 일련의 MenuItem 요소 및 Separator 요소로 구성됩니다. 각 MenuItem 요소는 컨텍스트 메뉴의 명령을 정의합니다. Header 특성은 메뉴 명령의 표시 텍스트를 정의하고, Click 특성은 각 메뉴 항목의 처리기 메서드를 지정합니다. Separator 요소는 이전 메뉴 항목과 이후 메뉴 항목 사이에 분리선이 렌더링되도록 하는 역할을 합니다.

<TextBox
  Name="cxmTextBox" 
  Grid.Row="1"
  AcceptsReturn="True"
  AcceptsTab="True"
  VerticalScrollBarVisibility="Visible"
  TextWrapping="Wrap"
>
  <TextBox.ContextMenu>
    <ContextMenu 
      Name="cxm"
      Opened="CxmOpened"
    >
      <MenuItem 
        Header="Cut"
        Name="cxmItemCut" 
        Click="ClickCut" 
      />
      <MenuItem 
        Header="Copy" 
        Name="cxmItemCopy"
        Click="ClickCopy" 
      />
      <MenuItem 
        Header="Paste"
        Name="cxmItemPaste"
        Click="ClickPaste" 
      />
      <Separator/>
      <MenuItem 
        Header="Select All"
        Name="cxmItemSelectAll"
        Click="ClickSelectAll" 
      />
      <MenuItem 
        Header="Select Current Line"
        Name="cxmItemSelectLine"
        Click="ClickSelectLine" 
      />
      <Separator/>
      <MenuItem 
        Header="Undo Last Action"
        Name="cxmItemUndo"
        Click="ClickUndo" 
      />
      <MenuItem 
        Header="Redo Last Action"
        Name="cxmItemRedo"
        Click="ClickRedo" 
      />
      <Separator/>
      <MenuItem 
        Header="Clear All Text"
        Name="cxmItemClear"
        Click="ClickClear" 
      />
    </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>

다음 예제에서는 컨텍스트 메뉴를 사용 및 사용 중지하는 코드와 앞의 컨텍스트 메뉴 정의에 대한 구현 코드를 보여 줍니다. Opened 이벤트를 사용하여 TextBox의 현재 상태에 따라 특정 명령이 동적으로 사용되거나 사용되지 않도록 합니다.

기본 컨텍스트 메뉴를 복원하려면 ClearValue 메서드를 사용하여 ContextMenu 속성의 값을 지웁니다. 컨텍스트 메뉴를 함께 사용 중지하려면 ContextMenu 속성을 null 참조(Visual Basic에서는 Nothing)로 설정합니다.

Private Sub MenuChange(ByVal sender As Object, ByVal args As RoutedEventArgs)
    Dim rb As RadioButton = CType(sender, RadioButton)
    If myGrid.Children.Contains(cxmTextBox) Then
        Select Case rb.Name
            Case "rbCustom"
                cxmTextBox.ContextMenu = cxm
            Case "rbDefault"
                'Clearing the value of the ContextMenu property
                'restores the default TextBox context menu.
                cxmTextBox.ClearValue(ContextMenuProperty)
            Case "rbDisabled"
                'Setting the ContextMenu propety to 
                'null disables the context menu.
                cxmTextBox.ContextMenu = Nothing
        End Select
    Else : Return
    End If
End Sub

Private Sub ClickPaste(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Paste()
End Sub
Private Sub ClickCopy(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Copy()
End Sub
Private Sub ClickCut(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Cut()
End Sub
Private Sub ClickSelectAll(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.SelectAll()
End Sub
Private Sub ClickClear(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Clear()
End Sub
Private Sub ClickUndo(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Undo()
End Sub
Private Sub ClickRedo(ByVal sender As Object, ByVal args As RoutedEventArgs)
    cxmTextBox.Redo()
End Sub
Private Sub ClickSelectLine(ByVal sender As Object, ByVal args As RoutedEventArgs)
    Dim lineIndex As Integer = cxmTextBox.GetLineIndexFromCharacterIndex(cxmTextBox.CaretIndex)
    Dim lineStartingCharIndex As Integer = cxmTextBox.GetCharacterIndexFromLineIndex(lineIndex)
    Dim lineLength As Integer = cxmTextBox.GetLineLength(lineIndex)
    cxmTextBox.Select(lineStartingCharIndex, lineLength)
End Sub
Private Sub CxmOpened(ByVal sender As Object, ByVal args As RoutedEventArgs)
    'Only allow copy/cut if something is selected to copy/cut.
    If cxmTextBox.SelectedText = "" Then
        cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = False
    Else
        cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = True
        'Only allow paste if there is text on the clipboard to paste.
        If Clipboard.ContainsText() Then
            cxmItemPaste.IsEnabled = True
        Else
            cxmItemPaste.IsEnabled = False
        End If
    End If
End Sub
private void MenuChange(Object sender, RoutedEventArgs ags)
{
    RadioButton rb = sender as RadioButton;
    if (rb == null || cxm == null) return;

    switch (rb.Name)
    {
        case "rbCustom":
            cxmTextBox.ContextMenu = cxm;
            break;
        case "rbDefault":
            // Clearing the value of the ContextMenu property
            // restores the default TextBox context menu.
            cxmTextBox.ClearValue(ContextMenuProperty);
            break;
        case "rbDisabled":
            // Setting the ContextMenu propety to 
            // null disables the context menu.
            cxmTextBox.ContextMenu = null;
            break;
        default:
            break;
    }

}

void ClickPaste(Object sender, RoutedEventArgs args)     { cxmTextBox.Paste(); }
void ClickCopy(Object sender, RoutedEventArgs args)      { cxmTextBox.Copy(); }
void ClickCut(Object sender, RoutedEventArgs args)       { cxmTextBox.Cut(); }
void ClickSelectAll(Object sender, RoutedEventArgs args) { cxmTextBox.SelectAll(); }
void ClickClear(Object sender, RoutedEventArgs args)     { cxmTextBox.Clear(); } 
void ClickUndo(Object sender, RoutedEventArgs args)      { cxmTextBox.Undo(); }
void ClickRedo(Object sender, RoutedEventArgs args)      { cxmTextBox.Redo(); }

void ClickSelectLine(Object sender, RoutedEventArgs args)
{
    int lineIndex = cxmTextBox.GetLineIndexFromCharacterIndex(cxmTextBox.CaretIndex);
    int lineStartingCharIndex = cxmTextBox.GetCharacterIndexFromLineIndex(lineIndex);
    int lineLength = cxmTextBox.GetLineLength(lineIndex);
    cxmTextBox.Select(lineStartingCharIndex, lineLength);
}

void CxmOpened(Object sender, RoutedEventArgs args)
{
    // Only allow copy/cut if something is selected to copy/cut.
    if (cxmTextBox.SelectedText == "")
        cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = false;
    else
        cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = true;

    // Only allow paste if there is text on the clipboard to paste.
    if (Clipboard.ContainsText())
        cxmItemPaste.IsEnabled = true;
    else
        cxmItemPaste.IsEnabled = false;
}

참고 항목

작업

방법: 상황에 맞는 메뉴로 맞춤법 검사 사용

개념

TextBox 개요

RichTextBox 개요