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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:49:27+00:00 2026-06-12T07:49:27+00:00

It seems like I read another question / answer on this site about this

  • 0

It seems like I read another question / answer on this site about this issue but I cannot recall what the answer was and now I cannot find the original post.

I am not a fan of the default error template in WPF. I understand how to change this error template. However, if I add some content to the end of, say, a textbox, the size of the textbox does not change and the added content will (potentially) get clipped. How do I alter the textbox (I believe the correct termonology is adorned element) in this scenario so that nothing gets clipped?

Here is the XAML for the error template:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <AdornedElementPlaceholder />
          <TextBlock Foreground="Red" Text="Error..." />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Here is the XAML for a couple of textboxes in the form:

<StackPanel>
  <TextBox Text="{Binding...}" />
  <TextBox />
</StackPanel>
  • 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-06-12T07:49:28+00:00Added an answer on June 12, 2026 at 7:49 am

    Here’s a solution adapted from Josh Smith’s article on Binding to (Validation.Errors)[0] without Creating Debug Spew.

    The trick is to define a DataTemplate to render the ValidationError object and then use a ContentPresenterto display the error message. If there is no error, then the ContentPresenter will not be displayed.

    Below, I have shared the code of the sample app that I created.

    Without errors With errors

    Here is the XAML:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            SizeToContent="WidthAndHeight"
            Title="MainWindow">
        <StackPanel Margin="5">
            <StackPanel.Resources>
                <DataTemplate DataType="{x:Type ValidationError}">
                    <TextBlock Text="{Binding ErrorContent}" Foreground="White" Background="Red" VerticalAlignment="Center" FontWeight="Bold"/>
                </DataTemplate>
            </StackPanel.Resources>
            <TextBox Name="TextBox1" Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
            <ContentPresenter Content="{Binding ElementName= TextBox1, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>
    
            <TextBox Name="TextBox2" Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
            <ContentPresenter Content="{Binding ElementName= TextBox2, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>
            <Button Content="Validate" Click="Button_Click"/>
        </StackPanel>
    </Window>
    

    The code behind file:

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private ViewModel _ViewModel = null;
    
            public MainWindow()
            {
                InitializeComponent();
                _ViewModel = new ViewModel();
                DataContext = _ViewModel;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                _ViewModel.Validate = true;
                _ViewModel.OnPropertyChanged("Text1");
                _ViewModel.OnPropertyChanged("Text2");
            }
        }
    }
    

    The ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace WpfApplication1
    {
        public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
        {
            private string _Text1;
            public string Text1
            {
                get { return _Text1; }
                set
                {
                    _Text1 = value;
                    OnPropertyChanged("Text1");
                }
            }
    
            private string _Text2;
            public string Text2
            {
                get { return _Text2; }
                set
                {
                    _Text2 = value;
                    OnPropertyChanged("Text2");
                }
            }
    
            public bool Validate { get; set; }
    
            #region INotifyPropertyChanged Implemenation
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
    
            #region IDataErrorInfo Implementation
            public string Error
            {
                get { return null; }
            }
    
            public string this[string columnName]
            {
                get
                {
                    string errorMessage = string.Empty;
                    if (Validate)
                    {
                        switch (columnName)
                        {
                            case "Text1":
                                if (Text1 == null)
                                    errorMessage = "Text1 is mandatory.";
                                else if (Text1.Trim() == string.Empty)
                                    errorMessage = "Text1 is not valid.";
                                break;
                            case "Text2":
                                if (Text2 == null)
                                    errorMessage = "Text2 is mandatory.";
                                else if (Text2.Trim() == string.Empty)
                                    errorMessage = "Text2 is not valid.";
                                break;
                        }
                    }
                    return errorMessage;
                }
            }
            #endregion
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this seems like a repeat question, but i've read all the others
Well is there? From everything I've read, it seems like the answer is no,but
Seems like this should be simple, but powershell is winning another battle with me.
Ive read about it and to be honest it all seems like a bunch
Seems like this should be obvious, but how do I send arrow key presses
I realise the info to answer this question is probably already on here, but
This question has been brought up many times, but I'd like to ask it
Seems like there should be... Right now it just seems like magic that you
Update: this question is specifically about protecting (encipher / obfuscate) the content client side
I just started using java so sorry if this question's answer is obvious. I

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.