Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676240
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:04:32+00:00 2026-05-19T03:04:32+00:00

I have one problem. i have dep. prop. of type List.. public const string

  • 0

I have one problem. i have dep. prop. of type List..

    public const string ValidationRulesPropertyName = "ValidationRules";


    public static List<ValidationRule> GetValidationRules(DependencyObject obj)
    {
        return (List<ValidationRule>)obj.GetValue(ValidationRulesProperty);
    }


    public static void SetValidationRules(DependencyObject obj, List<ValidationRule> value)
    {
        obj.SetValue(ValidationRulesProperty, value);
    }

    public static readonly DependencyProperty ValidationRulesProperty = DependencyProperty.RegisterAttached(
    ValidationRulesPropertyName,
    typeof(List<ValidationRule>),
    typeof(CustomGrid),
    new PropertyMetadata(new List<ValidationRule>()));

And now if I set in my custom grid some textboxes and inside one list of ValidationRules

<Grid>
<TextBox x:Name="txt1">
<ValidationRules>
<Validation:SomeValidationRule/>
</ValidationRule>
</TextBox>
<TextBox x:Name="txt2"/>
</Grid>

Ok. Now the problem is when I try to get list of rules for some element.. If have instances of txt1 and txt2 whe I get validation rules they both return instance of SomeValidationRule.

Grid.GetValidationRules(txt1Instance);

and

Grid.GetValidationRules(txt2Instance);

return same list.

Even if try

Grid.GetValidationRules(new TextBox());

I get the same list with SomeValidationRule as only alement in list. So that is strange. If I manually set list to some element then that element have that list that i set but all others element have list that i set up in xaml only for txt1.

Any idea? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:04:33+00:00Added an answer on May 19, 2026 at 3:04 am

    Try this modification:-

        public const string ValidationRulesPropertyName = "ValidationRules";
    
        public static List<ValidationRule> GetValidationRules(DependencyObject obj)
        {
            object result = obj.ReadLocalValue(ValidationRulesProperty);
            if (result == DependencyProperty.UnsetValue)
            {
                result = new List<ValidationRule>();
                obj.SetValue(ValidationRulesProperty, result);
            }
            return (List<ValidationRule>)result;
        }
    
    
        public static void SetValidationRules(DependencyObject obj, List<ValidationRule> value)
        {
            obj.SetValue(ValidationRulesProperty, value);
        }
    
        public static readonly DependencyProperty ValidationRulesProperty = DependencyProperty.RegisterAttached(
        ValidationRulesPropertyName,
        typeof(List<ValidationRule>),
        typeof(CustomGrid), null);
    

    This code removes the single instance of List created in the Metadata and defers the creation of a list until the first time GetValidationRules is called which in turn creates a List if one hasn’t already been created.

    When using PropertyMetaData only immutable types should be used as a default value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.