通过自己控制的验证错误

本文关键字:验证 错误 控制 自己 | 更新日期: 2023-09-27 18:08:40

我已经有了自己的用户控件:

[TemplateVisualState(Name = StateValid, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidFocused, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidUnfocused, GroupName = GroupValidation)]
public class SearchTextBoxControl : TextBox
{
    // properties removed for brevity
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.BindingValidationError += (s, e) => UpdateValidationState();
        this.UpdateValidationState();
    }
    public const string GroupValidation = "ValidationStates";
    public const string StateValid = "Valid";
    public const string StateInvalidFocused = "InvalidFocused";
    public const string StateInvalidUnfocused = "InvalidUnfocused";
    private void UpdateValidationState()
    {
        var textBox = this.GetTemplateChild("ContentTextBox");
        if (textBox != null)
        {
            VisualStateManager
                .GoToState(textBox as Control, 
                           Validation.GetErrors(this).Any() ? 
                               StateInvalidUnfocused : 
                               StateValid, 
                           true);
        }
    }
}
XAML:

<Style TargetType="local:SearchTextBoxControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:SearchTextBoxControl">
                <Grid Grid.Column="1"
                      Grid.ColumnSpan="3"
                      Grid.Row="1"
                      Margin="{TemplateBinding Margin}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="32" />
                    </Grid.ColumnDefinitions>
                    <TextBox x:Name="ContentTextBox"
                             Grid.ColumnSpan="2"
                             IsReadOnly="{TemplateBinding IsReadOnly}"
                             Text="{TemplateBinding Text}">
                    </TextBox>
                    <Button Grid.Column="1"
                            Style="{StaticResource BrowseButton}"
                            Command="{TemplateBinding Command}">
                        <ToolTipService.ToolTip>
                            <ToolTip Content="{TemplateBinding ToolTip}" />
                        </ToolTipService.ToolTip>
                        <Image  Source="../../Resources/Images/magnifier.png"
                                Style="{StaticResource BrowseButtonImage}" />
                    </Button>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我应该怎么做来传递验证错误到TextBox x:Name="ContentTextBox"验证服务(我想在我的控制文本框上相同的验证错误工具提示)?亲切的问候!

通过自己控制的验证错误

可以实现IDataErrorInfo Interface。它在componentmodellib中可用。

使用System.ComponentModel;

公共类SearchTextBoxControl: TextBox, IDataErrorInfo

{

    #region IDataErrorInfo Members
    public string Error
    {
        get { throw new NotImplementedException(); }
    }
    public string this[string columnName]
    {
        get { throw new NotImplementedException(); }
    }
    #endregion
    // Your Code

}