【问题标题】:Pass validation errors in own control在自己的控制中传递验证错误
【发布时间】:2011-09-16 09:38:27
【问题描述】:

我有自己的用户控制:

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

【问题讨论】:

  • 无论如何 - 你知道答案吗? :)
  • 不幸的是,可能没有。在 WPF 中,对于像这样简单的事情,我将实现 IDataErrorInfo,并执行 ValidatesOnDataErrors 事情。但我不确定 Silverlight 或使用视觉状态时是否支持。

标签: c# silverlight xaml mvvm


【解决方案1】:

您可以实现 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

}

【讨论】:

  • 这不是我一直在寻找的,但thanx :)
猜你喜欢
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2012-02-29
  • 2018-06-16
  • 2020-07-21
相关资源
最近更新 更多