I'm trying to change the IsEnabled property of a button if the validation on some of the textboxes in my form return true. So if there is errors, the IsEnabled property should be set to false.
For some reason I cannot get it to work. The IDataErrorInfo implementation is only called after my IsEmailValid property is called, so the Validation.GetHasError always returns false and my button is never disabled.
Can anybody help?
Code:
Textbox validated with IDataErrorInfo
<TextBox Style="{StaticResource textBoxInError}" Name="txtEmail" Grid.Column="1" Grid.Row="2" Width="150" Height="23" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBox.Text>
<Binding Path="Email" Mode="TwoWay"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="LostFocus"
></Binding>
</TextBox.Text>
</TextBox>
IDataErrorInfo Implementation:
public string Error
{
get
{
return null;
}
}
public string this[string name]
{
get
{
string result = null;
#region Email
if (name == "Email")
{
if (!presenter.LenientValidateEmail(Email))
{
result = "Your email address is not valid";
}
}
#endregion
#region Website
#endregion
return result;
}
}
Button binding on IsEnabled
<Button Name="btnUpdate" IsEnabled="{Binding IsValid}" HorizontalAlignment="Left" Grid.Column="3" Grid.RowSpan="2" Grid.Row="6" Height="23" Width="75" Click="btnUpdate_Click">Update
</Button>
public bool IsValid
{
get
{
return IsEmailValid();
}
}
public string Email
{
get
{
return _email;
}
set
{
_email = value;
OnPropertyChanged("Email"); // executes before IDataErrorInfo Implementation
}
}
private bool IsEmailValid()
{
object el = FindName("txtEmail");
if (el != null)
{
_isEmailValid = Validation.GetHasError((DependencyObject)el); // always false??
if (_isEmailValid)
{
return false;
}
else
return true;
}
return true;
}
//PropertyChanged event handler:
void ProfileView_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
IsEmailValid();
}