I was dabbling in WPF and noticed a peculiarity that I had never seen before. In the example below I have two text boxes that are bound to the same DP in the code-behind.
Code-behind:
public partial class MainWindow : Window
{
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(Window), new FrameworkPropertyMetadata("Hello"));
public MainWindow()
{
InitializeComponent();
}
}
And the XAML:
<TextBox>
<TextBox.Text>
<Binding RelativeSource = "{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<utils:RestrictInputValidator Restriction="IntegersOnly" ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Name="TextBox" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Mode=TwoWay, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
I noticed that when I type something in the TextBox that contains the IntegerOnly validation which fails the validation (in this case anything that is not an integer), the underlying Text variable does not update. Is this a default behavior? Why does it do this? Is it possible to override?