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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:17:55+00:00 2026-05-28T05:17:55+00:00

I am trying to make cutom validator controls but dont know why the Message

  • 0

I am trying to make cutom validator controls but dont know why the Message Property that i have on the ValidationRules that i wanted to bind to the templated parent. It works runs but everytime its empty. Dont know why its empty everytime.

The Sample project can be found here

STYLE

   <Style TargetType="{x:Type local:RequiredFieldBox}">
    <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:RequiredFieldBox}">
                <StackPanel Orientation="Vertical">

                    <TextBox>
                        <Binding  RelativeSource="{RelativeSource TemplatedParent}" Path="Text">
                        <Binding.ValidationRules>
                            <rule:RequiredFieldRule>
                                <rule:RequiredFieldRule.Params>
                                    <rule:ValidationParams 
                                        Message="{TemplateBinding Msg}"
                                        ValidationType="{TemplateBinding Type}"/>
                                </rule:RequiredFieldRule.Params>
                            </rule:RequiredFieldRule> 
                        </Binding.ValidationRules>
                    </Binding>
                        <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                      <Style.Triggers>
                           <Trigger Property="Validation.HasError" Value="true" >
                           <Setter Property="Foreground" Value="Red"/>
                           <Setter Property="Background" Value="MistyRose"/>
                           <Setter Property="BorderBrush" Value="Red"/>
                           <Setter Property="BorderThickness" Value="1.0"/>
                            <Setter Property="VerticalContentAlignment" Value="Center"/>
                           </Trigger>
                       </Style.Triggers>
                      <Setter Property="Validation.ErrorTemplate">
                         <Setter.Value>
                            <ControlTemplate>
                               <StackPanel>
                                  <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="40">
                                    <AdornedElementPlaceholder x:Name="Holder"/>
                                    <Image Height="{Binding Height, ElementName=Holder}" Width="20" Margin="10,0"
                                        Source="/Images/restricted.png" ToolTip="{Binding ElementName=Holder, 
                                        Path=AdornedElement.(Validation.Errors)
                     [0].ErrorContent}"/>
                                 </StackPanel>
                               </StackPanel>
                            </ControlTemplate>
                         </Setter.Value>
                    </Setter>
                 </Style>
             </TextBox.Style>
          </TextBox>
         </StackPanel>
        </ControlTemplate>
   </Setter.Value>
</Setter>

VALIDATION PARAMS CLASS

public class ValidationParams : DependencyObject
{

    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
      "Message",
      typeof(string),
      typeof(ValidationParams),
      new FrameworkPropertyMetadata(null));
    // Dependency Properties

    public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.Register("ValidationType",
                                                                                          typeof(RequiredFieldRule.EnmValidationType),
                                                                                          typeof(ValidationParams),
                                                                                          new FrameworkPropertyMetadata(RequiredFieldRule.EnmValidationType.FieldNotEmpty));
    public static readonly DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName",
                                                                                         typeof(string),
                                                                                         typeof(ValidationParams),
                                                                                         new FrameworkPropertyMetadata(string.Empty));
    public string FieldName
    {
        get { return (string)GetValue(FieldNameProperty); }
        set { SetValue(FieldNameProperty, value); }
    }
    // Properties
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    [Category("ValidationType")]
    public RequiredFieldRule.EnmValidationType ValidationType
    {
        get { return (RequiredFieldRule.EnmValidationType)GetValue(ValidationTypeProperty); }
        set { SetValue(ValidationTypeProperty, value); }

    }
}

CustomControl Class

public class RequiredFieldBox : Control
{

    static RequiredFieldBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(RequiredFieldBox), new FrameworkPropertyMetadata(typeof(RequiredFieldBox)));
    }
    public static readonly DependencyProperty MsgProperty = DependencyProperty.Register(
     "Msg",
     typeof(string),
     typeof(RequiredFieldBox),
     new FrameworkPropertyMetadata(null)
   );
    public enum EnmValidationType
    {
        FieldNotEmpty,
        FieldNumeric,
        FieldIPAddress
    }
    public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type",
                                                                                         typeof(RequiredFieldBox.EnmValidationType),
                                                                                         typeof(RequiredFieldBox),
                                                                                         new FrameworkPropertyMetadata(RequiredFieldBox.EnmValidationType.FieldNotEmpty));
    public EnmValidationType Type
    {
        get { return (EnmValidationType) GetValue(TypeProperty); }
        set { SetValue(TypeProperty, value);}
    }
    private static void MessageChanaged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        RequiredFieldBox sh = (RequiredFieldBox)d;
        if (sh.Msg != (string)e.OldValue)
            sh.Msg = (string)e.NewValue;
    }
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
    "Text",                               //Property name
    typeof( object ),                           //Property type
    typeof( RequiredFieldBox ));

    public string Msg
    {
        get { return (string)GetValue(MsgProperty); }
        set { SetValue(MsgProperty, value); }
    }

    public object Text
    {
        get { return GetValue(TextProperty);}
        set { SetValue(TextProperty, value); }
    }
}

Validation Rule Class

public class RequiredFieldRule : ValidationRule
{

    public enum EnmValidationType
    {
        FieldNotEmpty,
        FieldNumeric,
        FieldIPAddress
    }

    // Local variables and objects
    private ValidationParams mParams = new ValidationParams();

    public ValidationParams Params
    {
        get { return mParams; }
        set { mParams = value;}
    }

    // Override
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        ValidationResult objResult = null;
        string sValue = value as string;
        objResult = new ValidationResult(true, null);
        switch (Params.ValidationType)
        {
            case EnmValidationType.FieldNotEmpty:
                if (string.IsNullOrEmpty(sValue) == true)
                    return new ValidationResult(false, Params.Message);
                break;
            case EnmValidationType.FieldNumeric:
                int iValue = 0;
                if (int.TryParse(sValue, out iValue) == false)
                    objResult = new ValidationResult(false, Params.Message);
                break;
            case EnmValidationType.FieldIPAddress:
                break;
        }
        return objResult;
    }

}
  • 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-28T05:17:56+00:00Added an answer on May 28, 2026 at 5:17 am

    In a validation rule there is no context or tree connection, as a template binding is roughly equivalent to a RelativeSource binding it will not work. You might be out of luck…

    Usually the only thing that works is Source in combination with StaticResource, x:Reference or x:Static, as you cannot refer to the templated parent via name you maybe can tunnel it through another control, e.g. something like:

    <TextBox x:Name="RequiredFieldBox"
             Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}">
        <TextBox.Resources>
            <rule:RequiredFieldRule x:Key="rule">
                <rule:RequiredFieldRule.Params>
                    <rule:ValidationParams 
                        Message="{Binding Tag.Msg, Source={x:Reference RequiredFieldBox}}"
                        ValidationType="{Binding Tag.Type, Source={x:Reference RequiredFieldBox}}"/>
                </rule:RequiredFieldRule.Params>
            </rule:RequiredFieldRule>
        </TextBox.Resources>
        <!-- ... --->
             <Binding.ValidationRules>
                 <StaticResource ResourceKey="rule"/>
             </Binding.ValidationRules>
    

    (If the ValidationRule would be left in place the x:Reference in all likelihood would complain about cyclical dependency)


    An alternative using StaticResource and a tunnel element:

    <StackPanel Orientation="Vertical">
        <StackPanel.Resources>
            <FrameworkElement x:Key="Tunnel"
                    Tag="{Binding RelativeSource={RelativeSource AncestorType=local:RequiredFieldBox}}" />
        </StackPanel.Resources>
        <StaticResource ResourceKey="Tunnel" />
        <TextBox x:Name="RequiredFieldBox">
            <TextBox.Text>
                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Text">
                    <Binding.ValidationRules>
                        <rule:RequiredFieldRule>
                            <rule:RequiredFieldRule.Params>
                                <rule:ValidationParams
                                        Message="{Binding Tag.Msg, Source={StaticResource Tunnel}}"
                                        ValidationType="{Binding Tag.Type, Source={StaticResource Tunnel}}" />
                            </rule:RequiredFieldRule.Params>
                        </rule:RequiredFieldRule>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a custom :confirm message for a rails form that returns data
Im trying to make a custom view that will have a dynamic number of
I'm trying to make a custom infowindow. Version = 2. I have added my
Trying to make a make generic select control that I can dynamically add elements
I'm trying to make a set of custom tags that encapsulate form elements (markup
I'm trying to make a simple xml-editor for some basic but specific needs, the
I'm trying to make a custom swing control that is a meter. Swing Meter
I'm trying to make a custom FxCop rule that will test for calls to
I'm trying to make a custom ContentControl that takes on the shape of a
I'm trying to make a combo box that behaves somewhat like the Firefox 3

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.