[Learning] Chapter XXII WPF text controls

Original: [WPF] Chapter XXII learning text controls

  WPF provides three controls for entering text: TextBox, RichTextBox and PasswordBox. PasswordBox control directly inherited from the Control class. TextBox and RichTextBox controls indirectly inherited from TextBoxBase class.

  Different content control and saw earlier, can contain a content type text box is limited. TextBox controls are always stored string (provided by the Text property). PasswordBox control also handles character content (provided by the Password property), although in order to reduce the specific type of attack, it uses the property SecureString internally. Only RichTextBox control can store more complex content: FlowDocument objects can contain a complex combination of elements.

A multi-line text

  TextBox control typically stored single text (the number of characters may be limited by setting the MaxLength property). However, in many cases you need to handle large volumes of content, which will want to create a multi-line text box. For this case, the property to TextWrapping Wrap or WrapWithOverflow. If the property is set to TextWrapping Wrap, it will always wrap at the edge of control, setting a particularly long word in two rows. If the property is set to TextWrapping WrapWithOverflow, then if the line-breaking algorithm is found suitable location (such as a space or a hyphen) conversion lines, which allows to exercise some stretching beyond the right edge.

  In order to see the automatic multi-line text in the text box, the size needs to be set sufficiently large. When setting should not be hard-coded height (not adapt to such different font size, layout and may cause problems), and be easy to use MinLines MaxLines properties. MinLines attribute is the minimum number of rows to be displayed in the text box. For example, if the property value MinLines 2, will increase the height of the text box to the height of at least two rows. If space is insufficient container, part of the text box may be cut off. The maximum number of rows MaxLines property of the text box can display. Even if the extended text box to fit a container (e.g., the last element of a row or DockPanel proportionally changing the dimensions of the Grid control), does not exceed this limit.

  If the text box support wrap, the user may enter more text can be displayed immediately on the visual line. Therefore, by VerticalScrollBarVisibility property to Visible or Auto, add a scroll bar is always displayed or on-demand is significant (can also be set HorizontalScrollBarVisibility property to show unusual horizontal scroll bar).

  You might want to allow users to multi-line text box by pressing the Enter key to enter a hard carriage return (usually, in the text box and press the Enter key to trigger the default button). To ensure support for the Enter key text box, AcceptsReturn property needs to be set to true. AcceptsTabs properties may be provided to allow the user to insert the Tab key. Otherwise, Tab key The Tab key will move the focus to focus on the lower order to obtain a focus control.

  In some cases, it may be created purely for displaying text in the text box. This is can be IsReadOnly property to true to prevent editing text. Best by IsEnabled property to false to disable text box, because disabled text box will appear grayed-out text, does not support text selection (not support copying to the clipboard), and does not support scrolling.

Second, select the text

  In any text box can by clicking and dragging the mouse, or hold down the Shift key and using the arrow keys to move the text to select the text. TextBox class also provides the ability to use SelectionStart, SelectionLength and SelectedText properties, by which text programming decision to select or change the currently selected text.

  SelectionStart attribute determines the starting position of the selected text, this position is started from 0. For example, if the property is set to 10, the first character of the first selected character in the text box 11. SelectionLength selected character attribute indicates the total number (0 indicates no character is selected). Finally, SelectedText property can quickly check or change the selected text in the text box. It may be in response to changes in the text selection process by SelectionChanged event.

<Window x:Class="Controls.TextBoxTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBoxTest" Height="300" Width="300">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox  Name="txt" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" AcceptsReturn="True"  
             SelectionChanged="txt_SelectionChanged"  
             SpellCheck.IsEnabled="True">The Ministry of Truth contained, it was said, three thousand rooms above ground level, and corresponding ramifications below. Scattered about London there were just three other buildings of similar appearance and size. So completely did they dwarf the surrounding architecture that from the roof of Victory Mansions you could see all four of them simultaneously. They were the homes of the four Ministries between which the entire apparatus of government was divided. The Ministry of Truth, which concerned itself with news, entertainment, education, and the fine arts. The Ministry of Peace, which concerned itself with war. The Ministry of Love, which maintained law and order. And the Ministry of Plenty, which was responsible for economic affairs. Their names, in Newspeak: Minitrue, Minipax, Miniluv, and Miniplenty.</TextBox>
        <ScrollViewer Foreground="Blue" Grid.Row="1" Margin="0,10,0,5" VerticalScrollBarVisibility="Auto">

            <StackPanel>
                <TextBlock>Current selection:</TextBlock>
                <TextBlock  Name="txtSelection" TextWrapping="Wrap"></TextBlock>
            </StackPanel>
        </ScrollViewer>

    </Grid>
</Window>
TextBoxTest
private void txt_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (txtSelection == null) return;
            txtSelection.Text = String.Format(
                "Selection from {0} to {1} is \"{2}\"",
                txt.SelectionStart, txt.SelectionLength, txt.SelectedText);
        }
SelectionChanged

 

   TextBox class also provides properties AutoWordSelection controls the text selection behavior. If this property is set to true, then when you drag the mouse in the text box every time the text will select the entire word.

  Another useful feature is the TextBox control Undo, which allows users to undo the last operation. As long as no CanUndo the property set to false, the code can be obtained by Undo function (calling Undo () method), and using the obtained Ctrl + Z shortcut function.

Third, spell check

  TextBox provides a more special features - integrated spell checker, this feature will not be recognized in the text add a word that even the color of the wavy underline below. Right-click the user can not recognize the word, and select from the list of possible correct word.

  To enable the TextBox control spell check, the value set for an SpellCheck.IsEnabled dependency property, as shown below:

<TextBox SpellCheck.IsEnabled="True">...</TextBox>

  WPF Spell Checker is a unique feature, which does not rely on other software (such as Office). Spell check the configuration of keyboard input to decide which language dictionary. The default setting may be overwritten by the TextBox control Language property, the property type inherits from FrameworkElement, may also be provided in xml <TextBox> element: lang characteristics. However, WPF Spell Checker is currently confined in 4 languages: English, Spanish, French and German. SpellingReform property can be used to set whether after 1990 to modify the spelling rules apply to French and German.

  WPF allows you to add a series to be that there is no misspelled words (and used as a right-click recommendations in due course) to customize the dictionary. To do this, you must first create a dictionary file, but the dictionary file is a text file .lex expansion name. Add a list of words in the dictionary file. Each word is placed on a separate line, word order does not matter.

  Once you create a dictionary file, make sure SpellCheck.IsEnabled property of the TextBox control is set to true. Finally, use the associated SpellCheck.CustomDictionaries Uri object attribute points to the custom dictionary. If an alternate Uri object in XAML, it must first be introduced into the System namespace, as shown in the following example:

<Window x:Class="Controls.CustomSpellChecker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=system"
        Title="CustomSpellChecker" Height="300" Width="300">
    <Grid Margin="5">
        <TextBox SpellCheck.IsEnabled="True" TextWrapping="Wrap" 
 Text="Now the spell checker recognizes acantholysis and offers the right correction for acantholysi">
            <SpellCheck.CustomDictionaries>
                <sys:Uri>pack://application:,,,/Controls;component/CustomWords.lex</sys:Uri>
            </SpellCheck.CustomDictionaries>
        </TextBox>

    </Grid>
</Window>
CustomSpellChecker

Four, PasswordBox

  PasswordBox TextBox looks similar, but actually it is shielded by the display character string circle symbol (character can be selected by setting different PasswordChar shielding property). In addition, PassworBox control does not support the clipboard, and thus can not copy the text within.

  Compared with the TextBox class, PassworBox more streamlined user interface. TextBox class is very similar to that provided MaxLength property; Clear (), Paste () and the SelectAll () method; and providing a change event when the trigger text (PasswordChanged event). TextBox class and PassworBox most important difference is that the internal work. Although the properties can be used to read and set the text Password as normal strings, but only within the object System.Security.SecureString PasswordBox class.

  And general text is very similar, SecureString is a plain text object. The difference is storage in memory. SecureString in encrypted form stored in memory. Key used to encrypt a random string is generated, is stored in a never written to disk memory. The end result even if the computer crashes, the malicious user data it is impossible to retrieve the password by checking the page file. Even if found, only to find the encrypted version.

  SecureString class also provides the functionality necessary to discard the contents. When you call SecureString.Dispose () method, password data in memory will be overwritten. This ensures that all the cryptographic information is erased from the rewritable memory, and can not be reused in any manner. As expected, when the control is destroyed, PassworBox control will call Dispose () method automatically stored in memory SecureString object.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12285973.html