Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding DataContext to ValidationRule

Tags:

.net

binding

wpf

I have a custom ValidationRule that requires access to the ViewModel in order to validate a supplied value in conjunction with other properties of the ViewModel. I previously tried to acheive this by using a ValidationGroup, but abandoned this idea as the code I am modifying would need a lot of refactoring in order to enable this route.

I found a thread on a newsgroup that showed a way of binding the DataContext of a control in which the ValidationRule is being run to that ValidationRule by way of an intermediate class inherited from DependencyObject, but I cannot get it to bind.

Can anybody help?

My ValidationRule is as follows...

class TotalQuantityValidator : CustomValidationRule {

    public TotalQuantityValidator()
        : base(@"The total number must be between 1 and 255.") {
    }

    public TotalQuantityValidatorContext Context { get; set; }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo) {

        ValidationResult validationResult = ValidationResult.ValidResult;

        if (this.Context != null && this.Context.ViewModel != null) {

            int total = ...
            if (total <= 0 || total > 255) {
                validationResult = new ValidationResult(false, this.ErrorMessage);
            }

        }

        return validationResult;

    }

}

CustomValidationRule is defined as follows...

public abstract class CustomValidationRule : ValidationRule {

    protected CustomValidationRule(string defaultErrorMessage) {
        this.ErrorMessage = defaultErrorMessage;
    }

    public string ErrorMessage { get; set; }

}

TotalQuantityValidatorContext is defined as follows...

public class TotalQuantityValidatorContext : DependencyObject {

    public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(@"ViewModel",
        typeof(MyViewModel), typeof(TotalQuantityValidatorContext),
        new PropertyMetadata {
            DefaultValue = null,
            PropertyChangedCallback = new PropertyChangedCallback(TotalQuantityValidatorContext.ViewModelPropertyChanged)
        });

    public MyViewModel ViewModel {
        get { return (MyViewModel)this.GetValue(TotalQuantityValidatorContext.ViewModelProperty); }
        set { this.SetValue(TotalQuantityValidatorContext.ViewModelProperty, value); }
    }

    private static void ViewModelPropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs args) {
    }

}

And the whole thing is used thus...

<UserControl x:Class="..."
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:val="clr-namespace:Validators" x:Name="myUserControl">

    <TextBox Name="myTextBox">
        <TextBox.Text>
            <Binding NotifyOnValidationError="True" Path="myViewModelProperty" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <val:TotalQuantityValidator>
                        <val:TotalQuantityValidator.Context>
                            <val:TotalQuantityValidatorContext ViewModel="{Binding ElementName=myUserControl, Path=DataContext}" />
                        </val:TotalQuantityValidator.Context>
                    </val:TotalQuantityValidator>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

</UserControl>

The DataContext of the UserControl is being set to an instance of MyViewModel in code-behind. I know that this binding works as the standard control bindings are operating as expected.

The TotalQuantityValidator.Validate method is called correctly, but whenever I look at the ViewModel property of the Context, it is always null (the Context property of the TotalQuantityValidator is being set to an instance of TotalQuantityValidatorContext correctly). I can see from the debugger however that the setter on the ViewModel property of the TotalQuantityValidatorContext is never called.

Can anybody advise as to how I can get this binding to work?

Thanks in advance.

like image 255
Martin Robins Avatar asked Dec 03 '10 12:12

Martin Robins


1 Answers

I would avoid using validation rules. If you need access to the information in the viewmodel to perform validation, then it's better to put the validation logic in the viewmodel itself.

You can make your viewmodel implement IDataErrorInfo, and simply turn on data error info-based validation on the binding.

Even if you don't run into this (very common) problem of needing contextual information, validation rules aren't really a great way to express validation: validation rules are usually related to business logic, or at least to semantic aspects of your information. Xaml seems like the wrong place to put such things - why would I put a business rule in the source file whose main job is to determine the layout and visual design of my application?

Validation logic belongs further down in your app. Even the viewmodel might be the wrong layer, but in that case, you can simply make it the viewmodel's responsibility to work out where to find the validation logic.

like image 190
Ian Griffiths Avatar answered Sep 22 '22 21:09

Ian Griffiths