Before I was using my custom user control I was using a standard textxbox with the text property bound to a property of an item selected in a list box. The binding also used validation rules. After this property was set the text changed event was fired in the xaml and everything worked fine.
Now I have changed it to use a user control that contains a textbox. I added a dependency property to my user control that is bound to the text property of the textbox. Now the event is fired when the text is changed before the validation has occured. The validation does not occur until a checkbox in my dialog that is bound to another property of the same item is checked. A breakpoint on the set method in my model view is not being broken anymore other than on the inital selection in the listbox, however the value is being update as I am seeing the effect after I click on the textbox. My error message shown by the validation does not appear until the checkbox is clicked on either.
The XAML for the user control in my window:
<my:BoundTextBox x:Name="spacingValueBox" TextBoxBase.TextChanged="spacingValueBox_TextChanged">
<my:BoundTextBox.Text>
<Binding Path="SelectedItem.Property" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<validators:ValidationRule ErrorMessage="Invalid"/>
</Binding.ValidationRules>
</Binding>
</my:BoundTextBox.Text>
The Text property bound to my user control in the XAML:
<UserControl x:Class="ButtonTextBox" ...
... Name="control">
<TextBox Text="{Binding Text, ElementName=control}"/>
The dependency property:
public static readonly DependencyProperty TextProperty =
TextBox.TextProperty.AddOwner(typeof(BoundTextBox));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
The solution was actually surprisingly simple I just had to set the
UpdateSourceTriggervalue toPropertyChanged.