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

  • Home
  • SEARCH
  • 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 170079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:44:40+00:00 2026-05-11T12:44:40+00:00

I have a WPF window that uses validation. I created an error template that

  • 0

I have a WPF window that uses validation. I created an error template that puts a red border around an element that fails validation and displays the error message below. This works fine, but the error message is rendered on top of any controls beneath the control with the error. The best I can tell, this happens because the error template renders on the Adorner Layer, which is on top of everything else. What I’d like to have happen is for everything else to move down to make room for the error message. Is there a way to do this? All of the examples on the web seem to use a tool tip and use a simple indicator like an asterisk or exclamation point that doesn’t use much room.

Here is the template:

<ControlTemplate x:Key='ValidationErrorTemplate'>     <StackPanel>         <Border BorderBrush='Red' BorderThickness='2' CornerRadius='2'>             <AdornedElementPlaceholder x:Name='placeholder'/>         </Border>         <TextBlock Foreground='Red' FontSize='10' Text='{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent, FallbackValue=Error!}'></TextBlock>     </StackPanel> </ControlTemplate> 

Here are the controls using the template (I typed some of this out, so ignore any syntax errors):

<Grid>   <Grid.RowDefinitions>     <RowDefinition Height='Auto'/>     <RowDefinition Height='Auto'/>   </Grid.RowDefinitions>   <TextBox Name='Account' Grid.Row='0' Validation.ErrorTemplate='{StaticResource ValidationErrorTemplate}' Width='200'>     <TextBox.Text>       <Binding Path='AccountNumber'>         <Binding.ValidationRules>           <validators:RequiredValueValidationRule/>           <validators:NumericValidationRule/>         </Binding.ValidationRules>       </Binding>     </TextBox.Text>   </TextBox>   <TextBox Name='Expiration' Grid.Row='1' Validation.ErrorTemplate='{StaticResource ValidationErrorTemplate}' Width='100'  Margin='0,2,5,2'>     <TextBox.Text>       <Binding Path='ExpirationDate'>         <Binding.ValidationRules>           <validators:ExpirationDateValidationRule/>         </Binding.ValidationRules>       </Binding>     </TextBox.Text>   </TextBox> </Grid> 
  • 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. 2026-05-11T12:44:41+00:00Added an answer on May 11, 2026 at 12:44 pm

    EDIT: Alright, I’m not positive that this is the best solution (I sure hope someone can provide a better one), but here it goes:

    Instead of using the Validation.ErrorTeplate, which will present all of the visuals in the AdornerLayer, you can add some TextBlocks and bind them to Validation.HasError and (Validation.Errors)[0].ErrorContent, using a customer IValueConverter to convert the Validation.HasError bool to a Visibility value. It would look something like the following:

    Window1.cs:

    <Window x:Class='WpfApplicationTest.Window1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:sys='clr-namespace:System;assembly=mscorlib'     xmlns:local='clr-namespace:WpfApplicationTest'     Title='Window1' Height='300' Width='300'>     <Grid Margin='10'>         <Grid.Resources>             <!-- The person we are binding to -->             <local:Person x:Key='charles' Name='Charles' Age='20' />             <!-- The convert to use-->             <local:HasErrorToVisibilityConverter x:Key='visibilityConverter' />         </Grid.Resources>         <Grid.RowDefinitions>             <RowDefinition Height='Auto' />             <RowDefinition Height='Auto' />             <RowDefinition Height='Auto' />             <RowDefinition Height='Auto' />         </Grid.RowDefinitions>         <!-- The name -->         <TextBox Name='NameTextBox' Grid.Row='0' Text='{Binding Source={StaticResource charles}, Path=Name, ValidatesOnDataErrors=true}' />         <TextBlock Grid.Row='1'                     Foreground='Red'                     Text='{Binding ElementName=NameTextBox, Path=(Validation.Errors)[0].ErrorContent}'                     Visibility='{Binding ElementName=NameTextBox, Path=(Validation.HasError), Converter={StaticResource visibilityConverter}}' />          <!-- The age -->         <TextBox Name='AgeTextBox' Grid.Row='2' Text='{Binding Source={StaticResource charles}, Path=Age, ValidatesOnExceptions=true}' />         <TextBlock Grid.Row='3'                     Foreground='Red'                     Text='{Binding ElementName=AgeTextBox, Path=(Validation.Errors)[0].ErrorContent}'                     Visibility='{Binding ElementName=AgeTextBox, Path=(Validation.HasError), Converter={StaticResource visibilityConverter}}' />     </Grid> </Window> 

    Person.cs:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Text.RegularExpressions;  namespace WpfApplicationTest {     public class Person : IDataErrorInfo     {         public string Name { get; set; }         public int Age { get; set; }          #region IDataErrorInfo Members          string IDataErrorInfo.Error         {             get { throw new NotImplementedException(); }         }          string IDataErrorInfo.this[string columnName]         {             get             {                 switch (columnName)                 {                     case ('Name'):                         if (Regex.IsMatch(this.Name, '[^a-zA-Z ]'))                         {                             return 'Name may contain only letters and spaces.';                         }                         else                         {                             return null;                         }                     default:                         return null;                 }             }         }          #endregion     } } 

    HasErrorToVisibilityConverter.cs:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Data; using System.Windows;  namespace WpfApplicationTest {     [ValueConversion(typeof(bool), typeof(Visibility))]     public class HasErrorToVisibilityConverter : IValueConverter     {         #region IValueConverter Members          object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)         {             bool hasError = (bool)value;             return hasError ? Visibility.Visible : Visibility.Collapsed;         }          object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)         {             throw new NotImplementedException();         }          #endregion     } } 

    It doesn’t scale as well as having a single ControlTemplate that you can reference in all of your controls, but it’s the only solution I’ve found. I feel your pain – just about every example I can find on the topic of WPF validation is very simple, and almost always uses ‘!’ or ‘*’ preceding the control, with a tooltip bound to (Validation.Errors)[0].ErrorContent…

    Best of luck to ya! If I find a better solution, I’ll update this 😉

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

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using regular expressions is certainly not the best way to… May 11, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer It's possible but not desirable. Having shell access makes it… May 11, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer If the dead tuples have stacked up beyond what can… May 11, 2026 at 9:46 pm

Related Questions

I have a WinForm that uses an ElementHost to display a WPF UserControl. Once
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
I have a ComboBox in WPF which is databound, and has data template which
I have a visual brush which is a group of shapes, the main colour

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.