I would like to use the TextBox's 'Tag' property to store invalid characters (or eventually a Regex string) for validation of the text. In my simple example, I'm checking the TextBox text for any instances of 'A'.
I have my custom ValidationRule and DependencyObjects set up properly, such that:
This works:
<validators:CheckStringWrapper CheckString="A" />
But this does NOT work:
<validators:CheckStringWrapper CheckString="{Binding Tag,RelativeSource={RelativeSource Self}}" />
...giving the following error: System.Windows.Data Error: 40 : BindingExpression path error: 'Tag' property not found on 'object' ''CheckStringWrapper' (HashCode=6677811)'. BindingExpression:Path=Tag; DataItem='CheckStringWrapper' (HashCode=6677811); target element is 'CheckStringWrapper' (HashCode=6677811); target property is 'CheckString' (type 'String')
Full Code: XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- TextBox control with red border and icon image appearing at left with tool tip describing validation error -->
<ControlTemplate x:Key="ValidatedTextBoxTemplate">
<StackPanel Orientation="Horizontal">
<Image Height="16"
Margin="4,0,4,0"
Source="~/Resources/exclamation.png"
ToolTip="{Binding ElementName=ErrorAdorner,
Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" />
<Border BorderBrush="Red"
BorderThickness="1"
DockPanel.Dock="Left">
<AdornedElementPlaceholder Name="ErrorAdorner" />
</Border>
</StackPanel>
</ControlTemplate>
</ResourceDictionary>
<TextBox Name="MyTextBox"
Tag="A"
Validation.ErrorTemplate="{StaticResource ValidatedTextBoxTemplate}">
<Binding Mode="TwoWay"
NotifyOnValidationError="True"
Path="NewLotName"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnExceptions="True">
<Binding.ValidationRules>
<validators:InvalidFileNameChars />
<validators:InvalidPathChars />
<validators:CustomCharacterCheck>
<validators:CustomCharacterCheck.Wrapper>
<!-- This Works: <validators:CheckStringWrapper CheckString="A" /> -->
<validators:CheckStringWrapper CheckString="{Binding Tag,RelativeSource={RelativeSource Self}}" />
</validators:CustomCharacterCheck.Wrapper>
</validators:CustomCharacterCheck>
</Binding.ValidationRules>
</Binding>
</TextBox>
Classes:
public class CustomCharacterCheck : ValidationRule
{
public CheckStringWrapper Wrapper { get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string text = value.ToString();
if (text.Contains(Wrapper.CheckString))
{
return new ValidationResult(false, "Bad Char");
}
return ValidationResult.ValidResult;
}
}
public class CheckStringWrapper : DependencyObject
{
public static readonly DependencyProperty CheckStringProperty = DependencyProperty.Register("CheckString", typeof(string), typeof(CheckStringWrapper));
public string CheckString
{
get { return (string)GetValue(CheckStringProperty); }
set { SetValue(CheckStringProperty, value); }
}
}