예제

다음 XAML(Extensible Application Markup Language) 예제에서는 간단한 설명 TextBox 컨트롤인 tbFocusMe

XAML복사

<TextBox Name="tbFocusMe"> This is the text in my text box. </TextBox>

예제

다음 예제에서는 합니다 Focus 에 포커스를 설정 하는 방법의 TextBox 이름 사용 하 여 컨트롤 tbFocusMe합니다.

C#복사

tbFocusMe.Focus();

 

 

예제

내용을 수정 하지 못하게 하려면를 TextBox 컨트롤을 설정 합니다 IsReadOnly 특성을 true합니다.

XAML복사

<TextBox IsReadOnly="True" > The user may not modify the contents of this TextBox. </TextBox>

IsReadOnly 특성에 사용자 입력에만 영향을 줍니다;에 설정 된 텍스트에는 영향을 주지 않습니다를 XAML(Extensible Application Markup Language) 설명은 TextBox 컨트롤 또는 통해 프로그래밍 방식으로 설정 하는 텍스트를 Text 속성.

기본값인 IsReadOnly 됩니다 false합니다.

 

 

예제

다음 XAML(Extensible Application Markup Language) 코드에 설명 된 TextBox 제어 하 고 이름을 할당 합니다.

XAML복사

<TextBox Name="tbPositionCursor" > Here is some text in my text box... </TextBox>

예제

내용의 시작 부분에 커서를 배치 하는 TextBox 제어를 호출는 Select 메서드 선택 시작 위치 0의와 선택 길이를 0 지정 합니다.

C#복사

tbPositionCursor.Select(0, 0);

예제

내용의 끝에 커서를 TextBox 제어를 호출 합니다 Select 메서드 콘텐츠를 텍스트의 길이 선택 길이를 0 선택 시작 위치를 지정 합니다.

C#복사

tbPositionCursor.Select(tbPositionCursor.Text.Length, 0);

 

 

 

예제

XAML복사

<TextBox Name="tbSettingText"> Initial text contents of the TextBox. </TextBox>

예제

C#복사

tbSettingText.Text = "Initial text contents of the TextBox.";

 

 

다음 예제에서 실시간 맞춤법 검사를 사용 하도록 설정 하는 방법을 보여 줍니다는 TextBox 를 사용 하 여는 IsEnabled 의 속성을 SpellCheck 클래스.

예제

XAML복사

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <TextBox SpellCheck.IsEnabled="True" Name="myTextBox"></TextBox> </StackPanel> </Page>

C#복사

using System; using System.Windows; using System.Windows.Controls; namespace SDKSample { public partial class SpellCheckExample : Page { public SpellCheckExample() { StackPanel myStackPanel = new StackPanel(); //Create TextBox TextBox myTextBox = new TextBox(); myTextBox.Width = 200; // Enable spellchecking on the TextBox. myTextBox.SpellCheck.IsEnabled = true; // Alternatively, the SetIsEnabled method could be used // to enable or disable spell checking like this: // SpellCheck.SetIsEnabled(myTextBox, true); myStackPanel.Children.Add(myTextBox); this.Content = myStackPanel; } } }

 

 

 

 

예제

다음 XAML(Extensible Application Markup Language) 정의 하는 예제는 TextBox 사용자 지정 상황에 맞는 메뉴를 포함 하는 컨트롤입니다.

상황에 맞는 메뉴를 사용 하 여 정의 되는 ContextMenu 요소입니다. 일련의 구성 자체 상황에 맞는 메뉴 MenuItem 요소 및 Separator요소입니다.  MenuItem 요소는 상황에 맞는 메뉴 명령을 정의 합니다 Header 메뉴 명령에 대 한 표시 텍스트를 정의 하는 특성 및 Click 특성 각 메뉴 항목에 대 한 처리기 메서드를 지정 합니다. Separator 요소 수행 하면 이전 및 이후의 메뉴 항목 사이 렌더링할지 줄을 구분 합니다.

XAML복사

<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 참조 (Nothing Visual basic에서).

C#복사

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; }

+ Recent posts