【问题标题】:How to add a close button to ValidationSummary in Silverlight如何在 Silverlight 中向 ValidationSummary 添加关闭按钮
【发布时间】:2009-08-06 10:36:24
【问题描述】:

在 Silverlight 3 中,我正在使用 MVVM 以及验证原则,即如果发生验证错误,setter 会导致异常。我在使用 TwoWay 的字段上使用绑定语法,即:

<TextBox x:Name="TextBoxClientName" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=true}"  Grid.Column="1" Grid.Row="0" Margin="5 5 5 5" />

我使用注释在 ViewModel 中验证此属性:

[Required(ErrorMessage = "Name is required")]
public string Name
{
    get
    {
        return _client.Name;
    }
    set
    {
        Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name", DisplayName="Client Name" });
        _client.Name = value;
    }
}

我有验证摘要,一切正常,但是大声笑,我正在寻找的功能如下:

您有数据表单,我希望验证摘要仅在我单击保存时显示在顶部,此外,我希望在该 ValidationSummary 上实现一个关闭按钮,以便用户可以继续输入和更正。

我不确定如何使用验证摘要控制可见性或切换,我已经尝试了可见性。以下是我尝试过的代码,它确实收集了提交时的错误,但我无法将它们应用于验证摘要:

    public void Save()
    {
        List<ValidationError> errors = new List<ValidationError>();

        foreach (UIElement ui in LayoutRoot.Children)
        {
            FrameworkElement fe = ui as FrameworkElement;

            if (fe != null)
            {
                foreach (ValidationError ve in Validation.GetErrors(fe))
                {
                    errors.Add(ve);
                }
            }
        }


        if (errors.Count > 0)
        {

            Validation1.DataContext = errors;
            Validation1.Filter = ValidationSummaryFilters.All;
        }
        else
        {
            if (Saved != null)
                Saved(this, EventArgs.Empty);
        }

    }

干杯,

安德鲁

【问题讨论】:

    标签: c# silverlight


    【解决方案1】:

    我猜你现在在你的应用程序中使用了 SIlverlight 4。 所以这个答案适用于 Silverlight 4。

    在 Silverlight 4 中添加了一个新接口 INotifyDataError 和 3 种方法:

    public interface INotifyDataErrorInfo 
    {
        // Returns True if the object has at least one property-level or top-level error. 
        bool HasErrors { get; }
    
        // Returns the current set of property-level errors for the provided property name, or
        // the current top-level errors if the argument is null or empty. 
        IEnumerable GetErrors(string propertyName);
    
        // Raised when the set of errors for a particular property has changed, or when the 
        // top-level errors have changed. 
        event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    }
    

    网络上有大量关于该界面及其使用方法的文档。

    如果您不想使用,则不必使用 DataAnotations。但如果这样做,您仍然可以使用 System.ComponentModel.DataAnnotations 命名空间中的 Validator 类获取验证错误。

    如果您让 ViewModel 实现 INotifyDataError 并且还有一个属性(在 ViewModel 中)bool IsValidating 或类似的东西。然后,每次属性更改时,都会为您要验证的所有属性触发 ErrorsChanged 事件(您可以使用反射获取属性名称)。就是这样。

    现在您只需创建IsValidating = false,然后在请求保存时显示IsValidating = true 的错误。

    您可以做的其他事情(这适用于 Silverlight 3)是将 ValidationSummary 的 Visibility 绑定到 IsValidating 属性(使用 IValueConverter),然后从 ViewModel 控制它。

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      相关资源
      最近更新 更多