0

I am new to wpf
I want to create a custom textbox with the features such as

  • Change background color or Change Border Darker/Lighter on GotFocus and LostFocus.
  • Write only in UPPER CASE
  • WaterMark
  • Rounded Corner

I found code for rounded corner in XAML and it works good.
Following is the code :

<TextBox x:Name="txtUserName" BorderThickness="1" Height="23">
    <TextBox.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="3"/>
        </Style>
    </TextBox.Resources>
</TextBox>

I use to code in VB.NET Winforms before and I did something like this in there.
I made a class say MainTextBoxex

Public Class MainTextboxex
    Inherits TextBox

    Private Sub MainTextBoxEx_Enter(sender As Object, e As EventArgs) Handles Me.Enter
        Me.BackColor = Color.FromArgb(247, 180, 65)
        'Me.SelectAll()
    End Sub

    Private Sub MainTextBoxEx_Leave(sender As Object, e As EventArgs) Handles Me.Leave
        Me.BackColor = Color.White
    End Sub

    Public Sub New()
        MyBase.New()
        Me.CharacterCasing = CharacterCasing.Upper
    End Sub

    Private Sub MainTextboxex_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = ChrW(39) Then e.KeyChar = ChrW(96)
    End Sub

    Private NotInheritable Class NativeMethods
        Private Sub New()
        End Sub

        Private Const ECM_FIRST As UInteger = &H1500
        Friend Const EM_SETCUEBANNER As UInteger = ECM_FIRST + 1

        <DllImport("user32.dll", EntryPoint:="SendMessageW")>
        Public Shared Function SendMessageW(hWnd As IntPtr, Msg As UInteger, wParam As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As IntPtr
        End Function
    End Class

    Private _watermark As String
    Public Property Watermark() As String
        Get
            Return _watermark
        End Get
        Set(value As String)
            _watermark = value
            UpdateWatermark()
        End Set
    End Property

    Private Sub UpdateWatermark()
        If IsHandleCreated AndAlso _watermark IsNot Nothing Then
            NativeMethods.SendMessageW(Handle, NativeMethods.EM_SETCUEBANNER, CType(1, IntPtr), _watermark)
        End If
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        UpdateWatermark()
    End Sub
End Class

This worked awesome in winforms as I needed. (No rounded corners in there)
I just need to know how it is done here.
I want something that I don't have to code it everytime or for every Individual TextBox for the above feature everywhere.
Just use this custom TextBox and all set.
This will help me alot.

Thank You.

bonny
  • 688
  • 1
  • 14
  • 33
  • Regarding uppercase, set `TextBox.CharacterCasing` property. Regarding appearance, you are expected to edit TextBox's Style in WPF. See https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/textbox-styles-and-templates – emoacht Mar 31 '22 at 03:44
  • This will help. Will try and tell you. @emoacht – bonny Mar 31 '22 at 03:57
  • I think relatively difficult part is watermark but you can refer https://stackoverflow.com/questions/833943/watermark-hint-placeholder-text-in-textbox – emoacht Mar 31 '22 at 04:38

1 Answers1

0

Use UserControls and CustomControls depending on how much deep you want to dive in your customization. You can add your own properties by using "Dependency Property", and about the events you can expose Action Events and pass them through your Controls Action events.