I know this is an old post, but I use the markup extension to standardize my validation bindings. Thus, the advantage here is small, 4 of the default values ββthat I no longer need to set, and if I want to change them in the future, I only do it here.
using System; using System.Windows.Data; using System.Windows.Markup; namespace ITIS { /// <summary> /// Creates a normal Binding but defaults NotifyOnValidationError to True, /// ValidatesOnExceptions to True, Mode to TwoWay and /// UpdateSourceTrigger to LostFocus. /// </summary> public sealed class ValidatedBinding : MarkupExtension { public ValidatedBinding(string path) { Mode = BindingMode.TwoWay; UpdateSourceTrigger = UpdateSourceTrigger.LostFocus; Path = path; } public override object ProvideValue(IServiceProvider serviceProvider) { return new Binding(Path) { Converter = this.Converter, ConverterParameter = this.ConverterParameter, ElementName = this.ElementName, FallbackValue = this.FallbackValue, Mode = this.Mode, NotifyOnValidationError = true, StringFormat = this.StringFormat, ValidatesOnExceptions = true, UpdateSourceTrigger = this.UpdateSourceTrigger }; } public IValueConverter Converter { get; set; } public object ConverterParameter { get; set; } public string ElementName { get; set; } public object FallbackValue { get; set; } public BindingMode Mode { get; set; } public string Path { get; set; } public string StringFormat { get; set; } public UpdateSourceTrigger UpdateSourceTrigger { get; set; } } }
Martin Lottering
source share