标签:bst class source obj ESS 检验 rri 数据 new
Binding 中,有检验和转换关卡。
源码:
namespace System.Windows.Data
{
    public class Binding : BindingBase
    {
        ...
        public Collection<ValidationRule> ValidationRules { get; }    
        ...
    }
}
namespace System.Windows.Controls
{
    //
    // 摘要:
    //     提供创建自定义规则的一个方式,旨在检查用户输入的有效性。
    public abstract class ValidationRule
    {
        public abstract ValidationResult Validate(object value, CultureInfo cultureInfo);
    }
    
    //
    // 摘要:
    //     表示 ValidationRule.Validate(Object, CultureInfo)方法返回的结果。
    public class ValidationResult
    {
        public bool IsValid { get; }
        public object ErrorContent { get; }
    }
}
实例:
<Slider x:Name="sliderA" Minimum="0" Maximum="100"/>
<TextBox x:Name="textBoxA"/>
        
public class RangeValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        double d = 0;
        if (double.TryParse(value.ToString(), out d))
        {
            if (d >= 0 && d <= 10)
            {
                return new ValidationResult(true, null);
            }
        }
        return new ValidationResult(false, "Validation Failed");
    }
}
RangeValidationRule rvr = new RangeValidationRule();
Binding bindingS = new Binding("Value") { Source = this.sliderA };
bindingS.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bindingS.ValidationRules.Add(rvr);
this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);
源码:
namespace System.Windows.Controls
{
    public static class Validation
    {
        // 校验失败时触发的事件
        public static readonly RoutedEvent ErrorEvent;
    }
    
    //
    // 摘要:
    //     表示一个验证错误,该错误可通过 System.Windows.Controls.ValidationRule 报告验证错误时由绑定引擎创建
    public class ValidationError
    {
        public object ErrorContent { get; set; }
    }
}
实例:
RangeValidationRule rvr = new RangeValidationRule();
rvr.ValidatesOnTargetUpdated = true;
bindingS.ValidationRules.Add(rvr);
bindingS.NotifyOnValidationError = true;
this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);       
this.textBoxA.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
private void ValidationError(object sender, RoutedEventArgs e)
{
    if (Validation.GetErrors(this.textBoxA).Count > 0)
    {
        MessageBox.Show(Validation.GetErrors(this.textBoxA)[0].ErrorContent.ToString();
    }
}
namespace System.Windows.Data
{
    public interface IValueConverter
    {
        // Source To Target
        object Convert(object value, Type targetType, object parameter, CultureInfo culture);
        
        // Target To Source
        object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    }
}
public enum Category 
{
    Bomber,
    Fighter
}
public class CategoryToSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Category c = (Category)value; 
        switch (c)
        {
            case Category.Bomber:
                return @"llcons\Bomber.png"; 
            case Category.Fighter: 
                return @"\lcoos\Fighter.png";
            default: 
                return null;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<Window.Resources>
    <local:CategoryToSourceConverter x:Key="cts"/>
</Window.Resources>
<Image Widlh="20" Height="20"
       Source="{Binding Path=Category, Converter={StaticResoun cts}}"/>
标签:bst class source obj ESS 检验 rri 数据 new
原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14442848.html