Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i want to push some view model properties into validation class

i learnt that its difficult but possible so i have tried the fallowing

have declared a resource like the fallowing

<FrameworkElement x:Key="DataContextBridge" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />

and here is the validationrule

<TextBox x:Name="textBoxConferenceName" >
<Binding Path="CurrentConference.ConferenceName" >
<helper:ConferenceNameValidationRule ErrorMessage="ConferenceName Already Exists">
<helper:ConferenceNameUniqueValidationRuleData ConferenceCollection="{Binding Path=DataContext.ConferenceCollection, Source={StaticResource DataContextBridge}}"                                                                               ChoosenConference="{Binding Path=DataContext.SelectedConference, Source={StaticResource DataContextBridge}}" />

and here is the ConferenceNameValidationRule and ConferenceNameUniqueValidationRuleData C# classes

public class ConferenceNameUniqueValidationRule:ValidationRule
        public ConferenceNameUniqueValidationRule()
            _data = new ConferenceNameUniqueValidationRuleData();

        private ConferenceNameUniqueValidationRuleData _data;
        public ConferenceNameUniqueValidationRuleData Data
            get { return _data; }
            set { _data = value; }

        private string _errorMessage;
        public string ErrorMessage
            get { return _errorMessage; }
            set { _errorMessage = value; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            if (Data != null && (Data.ChoosenConference != null && Data.ConferenceCollection != null))
                if (Data.ConferenceCollection.Where(c => c.ConferenceID != Data.ChoosenConference.ConferenceID).Any(i => i.ConferenceName == Data.ChoosenConference.ConferenceName.Trim()))
                    return new ValidationResult(false,ErrorMessage);
            return ValidationResult.ValidResult;

public class ConferenceNameUniqueValidationRuleData:DependencyObject
        public ObservableCollectionEx<Conference> ConferenceCollection
            get { return (ObservableCollectionEx<Conference>)GetValue(ConferenceCollectionDp); }
            set { SetValue(ConferenceCollectionDp, value); }

        public Conference ChoosenConference
            get { return (Conference)GetValue(ChoosenConferenceDp); }
            set { SetValue(ChoosenConferenceDp, value); }

        public static readonly DependencyProperty ChoosenConferenceDp = DependencyProperty.Register(
            "ChoosenConference", typeof(Conference), typeof(ConferenceNameUniqueValidationRuleData), new PropertyMetadata(null));

        public static readonly DependencyProperty ConferenceCollectionDp = DependencyProperty.Register(
            "ConferenceCollection", typeof(ObservableCollectionEx<Conference>), typeof(ConferenceNameUniqueValidationRuleData), new PropertyMetadata(null));

so here i am using the datacontext object inside the validation class and i am also usind dependency object

and a i am getting the fallowing problem

the output shows fallowing error
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'FrameworkElement' (Name=''); target property is 'DataContext' (type 'Object')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900