पर काम नहीं कर रही है मेरे पास मेरे सिल्वरलाइट 4 एप्लिकेशन में एक साधारण परीक्षण पृष्ठ है जिसमें मैं एक कस्टम सत्यापन नियम आग लगाने की कोशिश कर रहा हूं।कस्टम वैलिडेशन विशेषता
मेरे पास टेक्स्टबॉक्स और एक बटन है, और मैं टेक्स्टब्लॉक में सत्यापन परिणाम दिखा रहा हूं। मेरे व्यू मॉडल में नाम संपत्ति है, जो टेक्स्टबॉक्स की टेक्स्ट प्रॉपर्टी को बाध्य करती है। मेरे पास नाम संपत्ति, [Required]
और [CustomValidation]
पर दो सत्यापन गुण हैं।
जब मैं सबमिट बटन दबाता हूं, तो आवश्यक सत्यापनकर्ता सही ढंग से आग लगाता है, लेकिन मेरे कस्टम सत्यापनकर्ता की सत्यापन विधि के अंदर ब्रेकपॉइंट कभी भी हिट नहीं होता है। मैं नहीं देख सकते हैं कि ऐसा क्यों है, के रूप में मुझे लगता है कि मैं एमएस के उदाहरण बहुत सावधानी से पालन किया है: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx
यहाँ दृश्य मॉडल के लिए कोड है:
<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;
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";
}
}
}
}
यहाँ दृश्य है
वास्तविक प्रश्न के लिए ऑर्थोगोनल, लेकिन एफडब्ल्यूआईडब्ल्यू आप Char.IsUpper स्थिर विधि के माध्यम से चेक कर सकते हैं: http://msdn.microsoft.com/en-us/library/system.char.isupper(v=VS.100) .aspx –