Skip to main content

WPF TextBox with Border Round Corner and Effect

WPF: Round Corner TextBox With Border effect


Introduction

This blog contains WPF XAML code snippet to create custom round corner TextBox in WPF.

Getting Started

This custom TextBox has three types of border effects which changes, when mouse overs over the TextBox, when gets focus and lost the mouse focus. It changes it's border color with mentions situations, when this TextBox losts mouse focus it comes in to normal mode. see the below images for more details.

Now we will discuss how we will get round corner textbox. We know that WPF provides various ways like using style,control template etc. to customize appearance of inbuild controls.

Here I have used WPF style and control template to customize the textbox appearance. Inside the template control of textbox, a border is added to display border with textbox and modified border thickness,border color and corner radious of border for displaying round corner.

For border effect of textbox, I used property trigger inside the style of textbox. This trigger will get actived when mouse cursor will enter into textbox or when textbox will get focuse or lost focuse.

Codes:


 <TextBox TextWrapping="Wrap" Name="txtMessage" Width="300" Height="40">  
       <TextBox.Style>  
         <Style TargetType="{x:Type TextBox}">  
           <Setter Property="Padding" Value="10"></Setter>  
           <Setter Property="Template">  
             <Setter.Value>  
               <ControlTemplate TargetType="{x:Type TextBox}">  
                 <Border Background="{TemplateBinding Background}"   
         x:Name="Bd" BorderBrush="Black"  
         BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">  
                   <ScrollViewer x:Name="PART_ContentHost"/>  
                 </Border>  
                 <ControlTemplate.Triggers>  
                   <Trigger Property="IsEnabled" Value="False">  
                     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>  
                     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>  
                   </Trigger>  
                   <Trigger Property="Width" Value="Auto">  
                     <Setter Property="MinWidth" Value="100"/>  
                   </Trigger>  
                   <Trigger Property="Height" Value="Auto">  
                     <Setter Property="MinHeight" Value="20"/>  
                   </Trigger>  
                   <Trigger Property="IsMouseOver" Value="True">  
                     <Setter TargetName="Bd" Property="BorderBrush" Value="Red"></Setter>  
                     <Setter TargetName="Bd" Property="BorderThickness" Value="3"></Setter>  
                   </Trigger>  
                   <Trigger Property="IsFocused" Value="True">  
                     <Setter TargetName="Bd" Property="BorderBrush" Value="DarkOrange"></Setter>  
                     <Setter TargetName="Bd" Property="BorderThickness" Value="2"></Setter>  
                   </Trigger>  
                 </ControlTemplate.Triggers>  
               </ControlTemplate>  
             </Setter.Value>  
           </Setter>  
         </Style>  
       </TextBox.Style>  
     </TextBox>  

Thanks
Kailash Chandra Behera

Comments

Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d