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 256481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:01:04+00:00 2026-05-11T22:01:04+00:00

I tried to write my own Validation rule for a ComboBox, the rule is

  • 0

I tried to write my own Validation rule for a ComboBox, the rule is attached to the binding for SelectedItem – However it dosn’t work. I’ve got similar rules working on the Text property …

<ComboBox VerticalAlignment="Top" ItemsSource="{Binding Animals}" DisplayMemberPath="Name" >
        <ComboBox.SelectedItem>
            <Binding Path="Animal">
                <Binding.ValidationRules>
                    <validators:ComboBoxValidationRule ErrorMessage="Please select an animal" />
                </Binding.ValidationRules>
            </Binding>
        </ComboBox.SelectedItem>
    </ComboBox>

I think its down to the code I’m using to call the validation which I found on the net. Basically SelectedItem isn’t coming up a dependency property.

It itterates through dependencyPropertyFields which contains TextProperty and SelectionBoxItemProperty but no SelectedItemProperty.

private void ValidateBindings(DependencyObject element)
    {
        Type elementType = element.GetType();

        FieldInfo[] dependencyPropertyFields = elementType.GetFields(
            BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly);


        // Iterate over all dependency properties
        foreach (FieldInfo dependencyPropertyField in dependencyPropertyFields)
        {
            DependencyProperty dependencyProperty =
                dependencyPropertyField.GetValue(element) as DependencyProperty;

            if (dependencyProperty != null)
            {


                Binding binding = BindingOperations.GetBinding(element, dependencyProperty);


                BindingExpression bindingExpression = BindingOperations.GetBindingExpression(element, dependencyProperty);

                // Issue 1822 - Extra check added to prevent null reference exceptions
                if (binding != null && bindingExpression != null)
                {


                    // Validate the validation rules of the binding
                    foreach (ValidationRule rule in binding.ValidationRules)
                    {
                        ValidationResult result = rule.Validate(element.GetValue(dependencyProperty),
                            CultureInfo.CurrentCulture);

                        bindingExpression.UpdateSource();

                        if (!result.IsValid)
                        {
                            ErrorMessages.Add(result.ErrorContent.ToString());
                        }

                        IsContentValid &= result.IsValid;
                    }
                }
            }
        }
    }

Does anyway know where I’m going wrong?

Any help greatly appreciated!

Thanks,

Andy

  • 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-11T22:01:04+00:00Added an answer on May 11, 2026 at 10:01 pm

    You aren’t finding the SelectedItemProperty, because ComboBox doesn’t have a SelectedItemProperty field, instead it is inheriting that from it’s base class Selector. Of course, Selector and ComboBox don’t have all the properties that you could possibly bind to either, you’ll have to traverse all the way back to UIElement to find most of the inherited properties.

    If you insert something to traverse the inheritance hierarchy then you can get all of the fields, and the validation rule will fire.

    List<FieldInfo> dependencyPropertyFields = elementType.GetFields(
        BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();
    
    // Iterate through the fields defined on any base types as well
    Type baseType = elementType.BaseType;
    while (baseType != null)
    {
        dependencyPropertyFields.AddRange(
            baseType.GetFields(
                BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly));
    
        baseType = baseType.BaseType;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried to write my own HttpModule (IHttpModule) that adds a Header like that:
I tried to write a function to check key code with JavaScript. It's working
I tried to write a script to list all files in directories and subdirectories
I tried to write a function that calculates a hamming distance between two codewords
I tried to write a Neural Network system, but even running through simple AND/OR/NOR
I tried to write a program that uses threads, but couldn't understand the o/p.
I tried to write a Taylor series expansion for exp(x)/sin(x) using fortran, but when
I've tried to write the smallest chunk of code to narrow down a problem.
I have tried to write my first Boost program from information on the Boost
I am learning emacs at the moment and tried to write an easy vhdl

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.