将TextBox.Tag传递给自己的ValidationRule

时间:2015-09-24 23:18:03

标签: wpf dependency-properties wcf-binding validationrule

我想使用TextBox' Tag'用于存储无效字符(或最终为正则表达式字符串)的属性,用于验证文本。在我的简单示例中,我正在检查TextBox文本中是否有' A'。

我正确设置了自定义ValidationRule和DependencyObjects,以便:

这有效:

<validators:CheckStringWrapper CheckString="A" />

但这不起作用:

<validators:CheckStringWrapper CheckString="{Binding Tag,RelativeSource={RelativeSource Self}}" />

...给出以下错误:System.Windows.Data错误:40:BindingExpression路径错误:&#39;标记&#39;在&#39; object&#39;上找不到的属性&#39;&#39; CheckStringWrapper&#39; (的HashCode = 6677811)&#39 ;. BindingExpression:路径=标签;的DataItem =&#39; CheckStringWrapper&#39; (的HashCode = 6677811);目标元素是&#39; CheckStringWrapper&#39; (的HashCode = 6677811);目标属性是&#39; CheckString&#39; (键入&#39; String&#39;)

完整代码: 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>

类:

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

    }
}

0 个答案:

没有答案