1

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); }

    }
}
  • I almost have this problem recreated. Can you add some more XAML so that it also shows the ValidatedTextBoxTemplate? – dev1998 Sep 25 '15 at 03:02
  • Hi dev1988... I added the template in the XAML. – CraigInCali Sep 25 '15 at 17:16
  • I have the problem recreated, but I don't have an answer yet. In the meantime this might help: [http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation] – dev1998 Sep 26 '15 at 18:26
  • I'm getting an error that says Wrapper.CheckString is null, whenever I try to bind it to something. I'll keep looking at this. – dev1998 Sep 28 '15 at 17:21

0 Answers0