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

The Archive Base Latest Questions

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

I have a simple test page in my Silverlight 4 application in which I’m

  • 0

I have a simple test page in my Silverlight 4 application in which I’m trying to get a custom validation rule to fire.

I have a TextBox and a Button, and I am showing the validation results in a TextBlock. My view model has a Name property, which is bound the the Text property of the TextBox. I have two validation attributes on the Name property, [Required] and [CustomValidation].

When I hit the Submit button, the Required validator fires correctly, but the breakpoint inside the validation method of my custom validator never gets hit. I can’t see why this is, as I think I have followed MS’s example very carefully: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx

Here is the code for the view model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using GalaSoft.MvvmLight.Command;

namespace MyProject
{
    // custom validation class
    public class StartsCapitalValidator
    {
        public static ValidationResult IsValid(string value)
        {
            // this code never gets hit
            if (value.Length > 0)
            {
                var valid = (value[0].ToString() == value[0].ToString().ToUpper());
                if (!valid)
                    return new ValidationResult("Name must start with capital letter");
            }
            return ValidationResult.Success;
        }
    }

    // my view model
    public class ValidationTestViewModel : ViewModelBase
    {
        // the property to be validated
        string _name;
        [Required]
        [CustomValidation(typeof(StartsCapitalValidator), "IsValid")]
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value, () => Name); }
        }

        string _result;
        public string Result
        {
            get { return _result; }
            private set { SetProperty(ref _result, value, () => Result); }
        }

        public RelayCommand SubmitCommand { get; private set; }

        public ValidationTestViewModel()
        {
            SubmitCommand = new RelayCommand(Submit);
        }

        void Submit()
        {
            // perform validation when the user clicks the Submit button
            var errors = new List<ValidationResult>();
            if (!Validator.TryValidateObject(this, new ValidationContext(this, null, null), errors))
            {
                // we only ever get here from the Required validation, never from the CustomValidator
                Result = String.Format("{0} error(s):\n{1}",
                    errors.Count,
                    String.Join("\n", errors.Select(e => e.ErrorMessage)));
            }
            else
            {
                Result = "Valid";
            }
        }
    }
}

Here is the view:

<navigation:Page x:Class="Data.Byldr.Application.Views.ValidationTest" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
    <Grid Width="400">
    <StackPanel>
        <TextBox Text="{Binding Name, Mode=TwoWay}" />
        <Button Command="{Binding SubmitCommand}" Content="Submit" />
        <TextBlock Text="{Binding Result}" />
    </StackPanel>
    </Grid>
</navigation:Page>
  • 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:30:53+00:00Added an answer on May 19, 2026 at 3:30 am

    As stated on the MSDN page for that overload of Validator.TryValidateObject ( http://msdn.microsoft.com/en-us/library/dd411803(v=VS.95).aspx ), only the object-level validations are checked with this method, and RequiredAttribute on properties.

    To check property-level validations, use the overload that also takes a bool ( http://msdn.microsoft.com/en-us/library/dd411772(v=VS.95).aspx )

    So it should be as simple as passing “true” as an extra parameter to TryValidateObject

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

Sidebar

Related Questions

I have a very simple silverlight xaml page displays textbox and a button. When
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have a simple little test app written in Flex 3 (MXML and some
I have an extremely simple unit test. I'm not sure why it fails when
I have recorded a very simple test case Using the Selenium IDE integrated with
I have a simple question related to one-line programming. First an example: function test(a)
I have a very simple question. I want to test whether a particular port
I have developed a simple Silverlight control. I created this by using the Silverlight
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
For example, suppose I have a batch file called 'test.cmd' and it simply contains:

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.