A user on the Blend/SketchFlow forums wanted to know if there was an easy way to add very basic validation for a form in a prototype. Below is a simple behavior that will validate that a text box is not empty, or if it matches a regex you provide, or both.
If you add this behavior code to your SL application in Blend, and change the namespace to match your project, and then build, this behavior will be available in the asset panel to drag out onto a TextBox. After you create it, you can select the behavior and edit its properties in the property panel. There you can set the regex that is validated, or set whether empty text is allowed.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace SilverlightApplication9
{
using System.Text.RegularExpressions;
public class SimpleValidationBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty RegexStringProperty = DependencyProperty.Register("RegexString", typeof(string), typeof(SimpleValidationBehavior), new PropertyMetadata(string.Empty));
public string RegexString
{
get { return GetValue(RegexStringProperty) as string; }
set { SetValue(RegexStringProperty, value); }
}
public static readonly DependencyProperty CanBeEmptyProperty = DependencyProperty.Register("CanBeEmpty", typeof(bool), typeof(SimpleValidationBehavior), new PropertyMetadata(false));
public bool CanBeEmpty
{
get { return (bool)GetValue(CanBeEmptyProperty); }
set { SetValue(CanBeEmptyProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
var tb = this.AssociatedObject;
if (tb != null)
{
tb.TextChanged += new TextChangedEventHandler(tb_TextChanged);
}
Validate();
}
protected override void OnDetaching()
{
base.OnDetaching();
var tb = this.AssociatedObject;
if (tb != null)
{
tb.TextChanged -= new TextChangedEventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, TextChangedEventArgs e)
{
// Validate text.
Validate();
}
private void Validate()
{
var tb = this.AssociatedObject;
var text = tb.Text;
bool emptyValid = this.CanBeEmpty ? true : !string.IsNullOrEmpty(text);
bool regexValid = string.IsNullOrEmpty(this.RegexString) ? true : Regex.IsMatch(text, this.RegexString);
bool valid = emptyValid && regexValid;
if (valid)
{
VisualStateManager.GoToState(tb, "Valid", false);
}
else
{
if (tb.Focus())
{
VisualStateManager.GoToState(tb, "InvalidFocused", true);
}
else
{
VisualStateManager.GoToState(tb, "InvalidUnfocused", true);
}
}
}
}
}
<TextBox Height="38" Margin="138,78,314,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" > <i:Interaction.Behaviors> <local:SimpleValidationBehavior RegexString="[0-9][0-9][a-z]"/> </i:Interaction.Behaviors> </TextBox>
November 2nd, 2010 on 10:25 am
Oh boy, I’m a designer, but have to implement form validation in Silverlight. I tried to use your code, but have to do something wrong, cuz nothing new appears in Assets (nor anywhere). This is how i do:
1) copy ‘n paste your code into my Main.xaml.cs, into public partial main class Main:Application; changing namespace of course
2) file -> new item -> behavior; similar: copy ‘n paste, changing namespace
Could you please explain me how to implement your solution?
November 2nd, 2010 on 11:44 am
Do you get an error when you try to compile now? It is hard to say exactly what the problem is, but it is possible you have a stray { or } someplace.
Replacing the contents of a new behavior is probably the best way to add it to your project, making the namespace match the one from your project.