1

我想使用 TextBox 的“标签”属性来存储无效字符(或最终是正则表达式字符串)以验证文本。在我的简单示例中,我正在检查 TextBox 文本中是否存在“A”的任何实例。

我正确设置了自定义 ValidationRule 和 DependencyObjects,例如:

这有效:

<validators:CheckStringWrapper CheckString="A" />

但这不起作用:

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

...给出以下错误:System.Windows.Data 错误:40:BindingExpression 路径错误:在“object”“CheckStringWrapper”(HashCode=6677811)上找不到“Tag”属性。绑定表达式:路径=标签;DataItem='CheckStringWrapper' (HashCode=6677811); 目标元素是“CheckStringWrapper”(HashCode=6677811);目标属性是“CheckString”(类型“String”)

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

    }
}
4

0 回答 0