How to set up a TextBox
to get only certain values. e.g. DateTime
input box with defined format settings.
How about using the Binding Validation that comes with the WPF Framework.
Create a ValidationRule like so
public class DateFormatValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var s = value as string;
if (string.IsNullOrEmpty(s))
return new ValidationResult(false, "Field cannot be blank");
var match = Regex.Match(s, @"^\d{2}/\d{2}/\d{4}$");
if (!match.Success)
return new ValidationResult(false, "Field must be in MM/DD/YYYY format");
DateTime date;
var canParse = DateTime.TryParse(s, out date);
if (!canParse)
return new ValidationResult(false, "Field must be a valid datetime value");
return new ValidationResult(true, null);
}
}
Then add it to your binding in xaml as well as a style to handle when the field is invalid. (You can also use Validation.ErrorTemplate if you're inclined to completely change the control.) This one puts the ValidationResult text as a tooltip and the box to red.
<TextBox x:Name="tb">
<TextBox.Text>
<Binding Path="PropertyThatIsBoundTo" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<val:DateFormatValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
A recommendation would be to take the style and put it into a resource dictionary so any textbox you want to have the same appearance when its own validation fails. Makes the XAML a lot cleaner too.
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
<TextBox Text="{Binding Path=Name}" />
And a function. This one just checks the string has content. Yours will be more complex depending on the exact format you want to enforce:
public string Name
{
get { return _name; }
set
{
_name = value;
if (String.IsNullOrEmpty(value))
{
throw new ApplicationException("Customer name is mandatory.");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With