gpt4 book ai didi

c# - CustomValidation 属性似乎不起作用

转载 作者:太空狗 更新时间:2023-10-29 20:47:49 24 4
gpt4 key购买 nike

我的 Silverlight 4 应用程序中有一个简单的测试页面,我试图在其中触发自定义验证规则。

我有一个 TextBox 和一个 Button,我在 TextBlock 中显示验证结果。我的 View 模型有一个 Name 属性,它绑定(bind)到 TextBox 的 Text 属性。我在 Name 属性上有两个验证属性,[Required][CustomValidation]

当我点击提交按钮时,必需的验证器正确触发,但我的自定义验证器的验证方法内的断点永远不会被命中。我不明白这是为什么,因为我认为我已经非常仔细地遵循了 MS 的示例:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx

这是 View 模型的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using GalaSoft.MvvmLight.Command;

namespace MyProject
{
// custom validation class
public class StartsCapitalValidator
{
public static ValidationResult IsValid(string value)
{
// this code never gets hit
if (value.Length > 0)
{
var valid = (value[0].ToString() == value[0].ToString().ToUpper());
if (!valid)
return new ValidationResult("Name must start with capital letter");
}
return ValidationResult.Success;
}
}

// my view model
public class ValidationTestViewModel : ViewModelBase
{
// the property to be validated
string _name;
[Required]
[CustomValidation(typeof(StartsCapitalValidator), "IsValid")]
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value, () => Name); }
}

string _result;
public string Result
{
get { return _result; }
private set { SetProperty(ref _result, value, () => Result); }
}

public RelayCommand SubmitCommand { get; private set; }

public ValidationTestViewModel()
{
SubmitCommand = new RelayCommand(Submit);
}

void Submit()
{
// perform validation when the user clicks the Submit button
var errors = new List<ValidationResult>();
if (!Validator.TryValidateObject(this, new ValidationContext(this, null, null), errors))
{
// we only ever get here from the Required validation, never from the CustomValidator
Result = String.Format("{0} error(s):\n{1}",
errors.Count,
String.Join("\n", errors.Select(e => e.ErrorMessage)));
}
else
{
Result = "Valid";
}
}
}
}

这是 View :

<navigation:Page x:Class="Data.Byldr.Application.Views.ValidationTest" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid Width="400">
<StackPanel>
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<Button Command="{Binding SubmitCommand}" Content="Submit" />
<TextBlock Text="{Binding Result}" />
</StackPanel>
</Grid>
</navigation:Page>

最佳答案

你为什么不像这样创建你自己的验证属性..

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class StartsCapital : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var text = value as string;

if(text == null)
return ValidationResult.Success;

if (text.Length > 0)
{
var valid = (text[0].ToString() == text[0].ToString().ToUpper());
if (!valid)
return new ValidationResult("Name must start with capital letter");
}
return ValidationResult.Success;
}
}

然后像这样使用它

 // my view model
public class ValidationTestViewModel : ViewModelBase
{
// the property to be validated
string _name;
[Required]
[StartsCapital]
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value, () => Name); }
}

关于c# - CustomValidation 属性似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4687422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com