Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

SmartTextBox

A WPF custom TextBox control with built-in input validation, built with Claude Opus 4.6.

.NETWPF

Overview

SmartTextBox is a reusable, extensible WPF CustomControl that inherits from TextBox with real-time input validation support. Validation logic is decoupled through the IValidator interface — swap the Validator in XAML, no control code changes required.

Built from scratch with 3 progressive prompts, each shorter than the last, because the architecture was right from the start.

Project Structure

SmartTextBoxApp/
├── DemoApp/ # WPF App — Validator demos
└── JamesSmartTextBox/ # Class Library
├── Properties/AssemblyInfo.cs
├── Local/Validators/IValidator.cs # IValidator interface + all Validators
├── Themes/Units/SmartTextBox.xaml # ControlTemplate
├── Themes/Generic.xaml
└── UI/Units/SmartTextBox.cs # CustomControl (inherits TextBox)

Core Design

6 DependencyProperties:

PropertyTypeDescription
HeaderstringLabel above the input field
PlaceholderstringWatermark text
CornerRadiusCornerRadiusBorder corner radius
ValidatorIValidatorValidation logic interface
IsValidboolWhether validation passed
ErrorMessagestringValidation error message

Validation Flow: TextChanged → Validator.Validate() → Update IsValid & ErrorMessage → ControlTemplate Triggers handle visual state

Built-in Validators

ValidatorDescription
RequiredValidatorNon-empty field check
EmailValidatorEmail format (regex)
PasswordValidator≥8 chars, uppercase + lowercase + digit
UrlValidatorHTTP/HTTPS format
PassportValidator1-2 letters + 6-9 digits
CreditCardValidatorLuhn algorithm

Usage

<smart:SmartTextBoxHeader="Credit Card"Placeholder="Enter card number"Margin="0,0,0,16">
<smart:SmartTextBox.Validator>
<validators:CreditCardValidator/>
</smart:SmartTextBox.Validator>
</smart:SmartTextBox>

To extend, simply implement IValidator — no changes to SmartTextBox required:

publicclassPhoneValidator:IValidator{publicstringMessage{get;set;}="Invalid phone number.";publicboolValidate(stringvalue)=>Regex.IsMatch(value??"",@"^\+?[\d\-\s]{10,15}$");}

AI Prompts

Step 1. Project Setup

Framework: .NET 10
Solution Name: SmartTextBoxApp
Output Format: ZIP file
Solution Structure:
- DemoApp (WPF App)
- JamesSmartTextBox (Class Library)
DemoApp:
- SmartTextBox examples in MainWindow.xaml
JamesSmartTextBox:
- SmartTextBox.cs (CustomControl, inherits TextBox)
- SmartTextBox.xaml (ResourceDictionary)
Project Structure:
1. UI/Units/SmartTextBox.cs
2. Themes/Units/SmartTextBox.xaml
3. Themes/Generic.xaml (Merge SmartTextBox.xaml)
Important:
- Include AssemblyInfo.cs
- Generic.xaml merges SmartTextBox.xaml
- SmartTextBox.cs sets DefaultStyleKey

Step 2. Add Validation

SmartTextBox DependencyProperty (6):
- Header (string)
- PlaceHolder (string)
- CornerRadius (CornerRadius)
- Validator (IValidator)
- IsValid (bool)
- ErrorMessage (string)
IValidator:
- Message (string)
- Validate(string value) → bool
Behavior:
- Auto-validate on Text change, show red border and ErrorMessage on invalid
Validator setup:
- IValidator + 2 Validators in same file
- Path: JamesSmartTextBox/Local/Validators/IValidator.cs
DemoApp:
- Apply Validators in MainWindow.xaml
Important:
- Only 6 DPs
- No comments/regions

Step 3. Built-in Validators

Add Validators inheriting IValidator:
- Email
- Password (min 8 chars, uppercase + lowercase + numbers)
- URL
- Passport
- CreditCard
DemoApp:
- Demonstrate all Validators in MainWindow.xaml

About

No description, website, or topics provided.

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages