I'm new to WPF and am trying to setup some validation for a textbox. I'm trying to determine if there's a way to set a custom type to a property through XAML by using a static method's return.
In my xaml, I currently have
<UserControl.Resources>
<ObjectDataProvider
ObjectType="{x:Type validators:StringValidator}"
MethodName="BasicValidator"
x:Key="basicValidator"/>
</UserControl.Resources>
...
<TextBox x:Name="StrTextBox" Width="200" Height="50" >
<TextBox.Text>
<Binding Path="TestText" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:StrValidationRule ValidatorType="{StaticResource basicValidator}"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Which throws the error on ValidatorType="{StaticResource basicValidator}"
An object of the type "System.Windows.Data.ObjectDataProvider" cannot be applied to a property that expects the type "Validator.StringValidator".
The ValidationRule is setup with a StringValidator property
public class StrValidationRule : ValidationRule
{
public StringValidator ValidatorType { get; set; }
...
}
I have a class that builds specific string validators which can be accessed through static methods. For example, the static method I'm trying to call is StringValidator.BasicValidator():
public class StringValidator : IValidator<string>
{
...
public static StringValidator BasicValidator()
{
whiteList = "abcde...";
return new StringValidator(whiteList);
}
public static StringValidator BinaryValidator()
{
whiteList = "01";
return new StringValidator(whiteList);
}
public static StringValidator NumericValidator()
{
whiteList = "-012345...";
return new StringValidator(whiteList);
}
}
And for the ValidationRule,
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string strValue = Convert.ToString(value);
return ValidatorType.Validate(strValue).Match (
Right: result => new ValidationResult(true, null),
Left: error => new ValidationResult(false, error));
}
I have tried using x:Static, but that appears to only handle properties. I'm also unsure if I need to go about this through Binding, but that route pops up many other issues.
Is there a simple fix that I'm simply unaware of, or is there a different approach that I need to follow to solve this?