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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:02:30+00:00 2026-06-07T09:02:30+00:00

I’ve been trying everything to figure this issue out. :( For a TextBox, I

  • 0

I’ve been trying everything to figure this issue out. 🙁

For a TextBox, I have a Validation.ErrorTemplate setup with an image on the right hand side of the textbox while it has a validation error.

This works great! But one thing I want to do is to resize or set the margin the textbox that has the error so it fits within the space the textbox has on the form. Currently, the image flows outside of the textboxes area.

What I really want is the textbox with error to take up the same space as textbox without.

Here is my XAML style:

<Style TargetType="{x:Type TextBox}">
<Style.Resources>
  <my:TextBoxWidthTransformConverter x:Key="TextBoxWidthTransformConverter"/>
</Style.Resources>
<Style.Triggers>
  <Trigger Property="Validation.HasError" Value="true">
    <Setter Property="Foreground" Value="Red"/>
    <Setter Property="Margin" Value="{Binding Converter={StaticResource TextBoxWidthTransformConverter}, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
  </Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
  <Setter.Value>
    <ControlTemplate>
      <StackPanel 
        Margin="{TemplateBinding Margin}"
        Orientation="Horizontal" 
        RenderOptions.BitmapScalingMode="NearestNeighbor"
        >              
        <AdornedElementPlaceholder 
          Grid.Column="1" 
          Grid.Row="1" 
          Name="controlWithError" 
          />
        <Border Width="2"/>
        <Image 
          ToolTip="{Binding ElementName=controlWithError, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" 
          Source="imagepath"
          />
      </StackPanel>
    </ControlTemplate>
  </Setter.Value>
</Setter>

I’m using a converter TextBoxWidthTransformConverter just to see if I can get something to happen, but before I was just using “0,0,20,0” in the Value, to no avail. The converter doesn’t fire, the Margin doesn’t change. I’ve used Snoop to see if I can see the property being touched or changed, but nothing happens.

Is Margin a property that can’t be changed by the Validation.HasError property?

Any insight would be wonderful!

Thanks!

  • 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-07T09:02:32+00:00Added an answer on June 7, 2026 at 9:02 am

    Hope to help out anybody who may run up against this issue.

    I’m still not sure why the Margin property is not changing during the Validation.HasError trigger, but I found a dirty work around.

    The workaround hijacks the Width property binding with some events (TextChanged and Unloading for cleanup of the events) using an IValueConverter to set the Margin. I preserve the original Margin, in my case I’m only worried about the right margin, by utilizing the TextBoxes Tag property.

    public class TextBoxMarginConverter : IValueConverter
    {
        private const double TEXTBOX_MARGIN_RIGHT = 25.0;
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          if (value == null)
          {
            return double.NaN;
          }
    
          TextBox textBox = value as TextBox;
    
          if (textBox == null)
          {
            return double.NaN;
          }
    
          if (Validation.GetHasError(textBox))
          {
            this.SetTextBoxMargin(TEXTBOX_MARGIN_RIGHT, textBox);
          }
    
          return textBox.Width;
        }
    
        private void SetTextBoxMargin(double marginRight, TextBox textBox)
        {
          if (textBox.Tag == null)
          {
            textBox.TextChanged += new TextChangedEventHandler(this.TextBoxTextChanged);
    
            textBox.Unloaded += new RoutedEventHandler(this.TextBoxUnloaded);
    
            double right = textBox.Margin.Right + marginRight;
    
            textBox.Tag = textBox.Margin.Right;
    
            textBox.Margin = new Thickness(textBox.Margin.Left, textBox.Margin.Top, right, textBox.Margin.Bottom);
          }
        }
    
        private void TextBoxUnloaded(object sender, RoutedEventArgs e)
        {
          TextBox textBox = sender as TextBox;
    
          if (textBox == null)
          {
            return;
          }
    
          textBox.TextChanged -= new TextChangedEventHandler(this.TextBoxTextChanged);
    
          textBox.Unloaded -= new RoutedEventHandler(this.TextBoxUnloaded);
        }
    
        private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
          TextBox textBox = sender as TextBox;
    
          if (textBox == null)
          {
            return;
          }
    
          if (Validation.GetHasError(textBox))
          {
            this.SetTextBoxMargin(TEXTBOX_MARGIN_RIGHT, textBox);
    
            return;
          }
    
          if (textBox.Tag != null)
          {
            double tag;
    
            double.TryParse(textBox.Tag.ToString(), out tag);
    
            textBox.Tag = null;
    
            textBox.Margin = new Thickness(textBox.Margin.Left, textBox.Margin.Top, tag, textBox.Margin.Bottom);
          }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          throw new NotImplementedException();
        }
    }
    

    In your Validation.ErrorTemplate, apply the converter to Validation.HasError trigger:

    <Style.Triggers>
      <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="Width" Value="{Binding Converter={StaticResource TextBoxMarginConverter}, RelativeSource={RelativeSource Self}}"/>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
      </Trigger>
    </Style.Triggers>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.