Wednesday, October 15, 2008, 02:30 PM
Posted by Ruben Steins
'Spotlights' are something new for me. I'm going to try and focus on a single WPF control once a week and make an extensive post about it.Posted by Ruben Steins
The WPF TextBox
It seems a bit silly to talk about such a simple and basic control as the WPF TextBox. After all, we see these things all the time. You add them to you favorite container and they show editable text, or according to the MSDN Documentation:
[TextBox] represents a control that can be used to display or edit unformatted text.
.gif)
Usage is simple:
<Grid>
<TextBox x:Name="MyTextBox">Hello World</TextBox>
</Grid>
You can also add color and font information to fit it into your look-and-feel:
<Grid>
<TextBox x:Name="MyTextBox" Background="LightGray"
Foreground="DarkBlue" FontFamily="Calibri"
FontSize="12" FontWeight="Bold">
Fancy Hello World
</TextBox>
</Grid>
But, there might be some interesting things you might not know about the TextBox.
Undo functionality
TextBox supports Undo-functinality out-of-the-box. You can simply call Undo() from code and the latest change to the text gets reverted. In fact, by default the UndoLimit property (inherited from TextBoxBase) is set to -1, which means 'infinite undo'. In practice this means there is a potential memoryleak there, since the undo-stack will keep on expanding as long as there's memory available in your system (read a nice solution that will prevent this from happening automagically)
Spell-checking
Textbox support spell-checking as well, out-of-the-box.
<TextBox SpellCheck.IsEnabled="True" />
The code above is enough to make this feature work. Currently it's not possible to specify a custom dictionary but still, so you have to have Office 2003 or higher installed (I think) to make it work properly. Look at this Spelling Suggestions in a WPF TextBox for some additional information and a nice implementation.
Support for Copy-Cut-Paste
Some WPF controls have a couple of commands associated with them by default. TextBox is one of them and supports Copy, Cut and Paste (and Undo and Redo as well).
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Orientation="Horizontal" Height="25">
<Button Command="Cut" CommandTarget=
"{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},
Path=Command.Text}"/>
<Button Command="Copy" CommandTarget=
"{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},
Path=Command.Text}"/>
<Button Command="Paste" CommandTarget=
"{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},
Path=Command.Text}"/>
<Button Command="Undo" CommandTarget=
"{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},
Path=Command.Text}"/>
<Button Command="Redo" CommandTarget=
"{Binding ElementName=textBox}"
Content="{Binding RelativeSource={RelativeSource Self},
Path=Command.Text}"/>
<TextBox x:Name="textBox" Width="200"/>
</StackPanel>
This examples is taken from a sample chapter for Adam Nathans 'WPF Unleashed' which is an awesome book. It shows how simple it is to create a fully functional Cut/Copy/Paste textbox. The best thing about it the fact that the buttons enable or disable based on whether or not there is text on the clipboard. You automatically get CanExecute() methods.
So, these are a couple of intersting tid-bits about the WPF TextBox. I'll list some more in a follow-up post since I'm a bit short on time right now!




( 2.9 / 63 )

Calendar



