在自己的控制中传递验证错误

发布于 2024-12-04 22:15:22 字数 2960 浏览 0 评论 0原文

我有自己的用户控件:

[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" 验证服务(我希望控件文本框中有相同的验证错误工具提示)? 亲切的问候!

I've got my own user control:

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

and 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>

What should I do to pass validation errors to the TextBox x:Name="ContentTextBox" validation service (I want the same validation error tooltip on my control text box)?
Kind regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-12-11 22:15:22

您可以实现 IDataErrorInfo 接口。它可以在 ComponentModel Lib 中找到。

使用 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

}

You can implement IDataErrorInfo Interface. It is available in ComponentModel Lib.

using System.ComponentModel;

public class 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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文