IDataErrorInfo and WPF

Posts   
 
    
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 20-Feb-2009 19:56:41   

I am in the process of building a WPF application and am having trouble with GUI validation using LLBlGen entities.

From what I have read, I should be able to add a DataErrorValidationRule to the Binding.ValidationRules of a binding, and this will make it so that if the bound entities has errors exposed through IDataErrorInfo, the Validation.HasError will trigger a tooltip...this is done with the following XAML style:


   <Style x:Key="textStyleTextBox" TargetType="TextBox">
        <Setter Property="Foreground" Value="#333333" />
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

I have added the DataErrorValidationRule to the validation rules on the binding to a textbox that I have which uses the style above.

From code-behind, I have tried calling SetEntityError and SetEntityFieldError on the bound entity, but this does not trigger the style trigger mentioned above.

I have also tried:


 MyTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource()

in order to force the control to validate, but this does not help. Anyone know what the problem here could be?

EDIT: Sorry, I seem to have a habit of figuring out the problem after posting flushed

I made a method on my entity which calls OnPropertyChanged. This raises the INotifyPropertyChanged event which causes the bound control to revalidate. So, to force an outside IDataErrorInfo field error and have WPF see this error (and trigger a popup window in my case), I need two lines:

myEntity.SetEntityFieldError("MyPropertyName", "the error message", true)
myEntity.ForceNotify("MyPropertyName")

ForceNotify is just:

Public Sub ForceNotify(ByVal fieldName As String)
            MyBase.OnPropertyChanged(fieldName)
End Sub

So there it is!