WPF 4.0 TextBox SelectionBrush and Opacity Effect

With the release of VS2010 and .NET 4.0, some new changes have been introduced in WPF 4.0 for providing UX. WPF 4.0 TextBox control has introduced new properties for text selection as listed below:

  • SelectionBrush - This defines the brush when the text in the textbox is selected.
  • SelectionOpacity - This defines the opacity of the of the selection color during highlighting the text.

In the following XAML I have used the textbox with some text inside it. The SelectionBrush is set to the ‘LightBlue’ and the SelectionOpacity is set to 0.6 for transparency.

<TextBox Height="170"
HorizontalAlignment="Left" Margin="46,43,0,0"
Name="txtDisplay" VerticalAlignment="Top" Width="336"
Text="Windows Presentation Foundation 4.0" FontSize="18"
SelectionBrush="LightBlue" SelectionOpacity="0.6" Foreground="Black"/>

When you run the application and select the text in the textbox, the following result will be displayed.

image






About The Author

Mahesh Sabnis is a Microsoft MVP having over 18 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at DotNetCurry.com. Follow him on twitter @maheshdotnet

No comments: