DataGrid RowValidationRule - Using Validation.GetHasError to set a DependencyProperty

Feb 8, 2010 at 6:03 PM

Hi,

Firstly I am using the DataGrid from the WPFToolKit on .NET 3.5 SP1.

I have the scenario where I want to disable my Save Button when anything on my page is invalid.  I decided to use a DependencyProperty, IsValid to do this and wire the buttons "IsEnabled".  Specifically I want to detect when a DataGridRow is invalid.  I have successfully used a RowValidationRule to check the row's validity, and this marks up the row as desired, but I cannot find a suitable event to check the validity of a rule after the validation rule has run using Validation.GetHasError.  I have tried:

DataGrid.RowEditEnding

DataGrid.SourceUpdated

DataGridRow.SourceUpdated

All of which don't report an error.  I tried to wire-up the Validation.Errors attached event on the DataGridRow, but this never seems to get triggered, and I cannot see where to use the NotifyOnValidationError=True in a binding.

Please can anyone suggest a solution to this or suggest what I might be doing wrong?

Kinds Regards,
Peter. 

Feb 9, 2010 at 4:44 PM
Edited Feb 9, 2010 at 5:59 PM

Hi,

I have managed to come up with a bit of a bodge workaround to solve this problem.

I have written a custom attached property; IsValid, and a custom attached event; ValidityChanged.  In the IsValid property change call back I raise the ValidityChanged event.  In the XAML I have an event setter for the DataGridRow style for the ValidityChanged attached event, and a style trigger when Validation.HasError is true sets the IsValid property to false.  XAML looks like this: 

<!-- DataGridRow Style -->

<Style TargetType="{x:Type dg:DataGridRow}">

<EventSetter Event="ext:ValidityChecker.ValidityChanged" Handler="EventPermissionRow_ValidityChanged"/>

<Setter Property="Validation.ErrorTemplate" Value="{StaticResource BasicRowValidationErrorTemplate}" />

      <Style.Triggers>

            <Trigger Property="Validation.HasError" Value="true">

                  <Setter Property="ext:ValidityChecker.IsValid" Value="false"/>

                  <Setter Property="BorderBrush" Value="Red"/>

<Setter Property="BorderThickness" Value="2"/>

                  <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />

</Trigger>

</Style.Triggers>

</Style>

I think this is quite a generic solution i.e. would work in any scenario with ValidationRules, but I think the normal binding validations rules, and NotifyOnValidationError=True will work fine in most cases.

Kinds Regards,
Peter.