Stackoverflow, TextBox, UpdateSourceTrigger, PropertyChanged, RelativeSource Self

For lack of a better title, I am sure someone out there tried to perform validation work on a textbox where it was bound to itself.  Follow the code sample if you will:

      <Binding RelativeSource="{RelativeSource Self}" Path="Text" UpdateSourceTrigger="PropertyChanged" />

Once you type something in the text box, whammo!  StackOverflowException.  See: for reasons why.

The easiest way to get around this is to set the UpdateSourceTrigger to "LostFocus,"  the default setting.  However, this leads into some hackery causing you to force validation by focusing on the control property. 


If you really want a validation on every character entered, you can approach this is by creating a private string dependancy property, and using a border to simulate the red outlining.

public static readonly DependencyProperty MyStringProperty = DependencyProperty.Register(

public String MyString
     get { return (String ).GetValue(Window1.MyStringProperty ); }
     set { SetValue(Window1.MyStringProperty , value); }

public Window1()
textBox1.TextChanged += delegate(object sender, RoutedEventArgs e)
          if (Valid())
               border.BorderBrush = null;

               border.BorderBrush = Brushes.Red;




Comments (1)

Comments are closed.

Skip to main content