This project has moved. For the latest updates, please go here.

Not able to display Validation Errors on PropertyGrid

Apr 19, 2012 at 8:03 AM

Hi,

I want to be able to display a tooltip on the property editor when there is a validation failure.

In order to do this for a particular property I have created an EditorDefinition as follows;

<xctk:EditorDefinition TargetType="{x:Type system:Int32}">
    <xctk:EditorDefinition.EditorTemplate>
        <DataTemplate>
            <xctk:IntegerUpDown Style="{StaticResource ToolkitUpDownStyle}"
                                Value="{Binding Value, ValidatesOnDataErrors=True}" />
        </DataTemplate>
    </xctk:EditorDefinition.EditorTemplate>
</xctk:EditorDefinition>

The style being used by the IntegerUpDown control is;

<Style x:Key="ToolkitUpDownStyle"
        TargetType="{x:Type xctk:IntegerUpDown}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError"
                    Value="true">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

This style works if I use it on an IntegerUpDown control outside of the property grid, but when I use it inside the grid the property cell displays a red border but no tooltip is displayed.

Further investigation reveals that the control thinks that there are no validation errors (Validation.Errors.Count = 0), even though the property grid cell has a red border.

Do you know why this is happening?

Thanks

Matt

 

 

Apr 19, 2012 at 3:34 PM

What happens if you try

Path=(Validation.Errors).CurrentItem.ErrorContent}
src

Apr 19, 2012 at 4:02 PM

I tried it and it didn't work.

To be honest I am not surprised because even though the property is invalid, the count of validation errors is 0 - so I would not expect there to be any error content.

Coordinator
Apr 20, 2012 at 7:39 PM
Edited Apr 20, 2012 at 7:39 PM

Hi,

 

The IntegerUpDown that you are setting is binded to the Value property of one PropertyItem of the PropertyGrid.  That PropertyItem's Value property is itself binded to one of the "selected object" property.  When a validation error occurs, the PropertyItem control is notified from the binding between the PropertyItem.Value property and the target object property.  This explains why a red border is shown around the PropertyItem control.  Unfortunately, no error is raised for the binding between the IntegerUpDown.Value property and the PropertyItem.Value property.  If you want your tooltip to work, here are some workarounds.

 

Set the tooltip on the PropertyItem control instead.  The tooltip will also be activated on the property name part.

<Style TargetType="{x:Type xctk:PropertyItem}">
   <Style.Triggers>
      <Trigger Property="Validation.HasError"
               Value="True">
         <Setter Property="ToolTip"
                  Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" />
      </Trigger>
   </Style.Triggers>
</Style>

 

Retrieve the validation error from the target PropertyItem.

<Style x:Key="ToolkitUpDownStyle"
       TargetType="{x:Type xctk:IntegerUpDown}">
   <Style.Triggers>
      <DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xctk:PropertyItem}}}"
                   Value="True">
         <Setter Property="ToolTip"
                 Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xctk:PropertyItem}}}" />
      </DataTrigger>
   </Style.Triggers>
</Style>

 or

<Style x:Key="ToolkitUpDownStyle"
       TargetType="{x:Type xctk:IntegerUpDown}">
   <Style.Triggers>
      <DataTrigger Binding="{Binding Path=DataContext.(Validation.HasError), RelativeSource={x:Static RelativeSource.Self}}"
                     Value="True">
         <Setter Property="ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=DataContext.(Validation.Errors).CurrentItem.ErrorContent}" />
      </DataTrigger>   
   </Style.Triggers>
</Style>

 

Apr 23, 2012 at 7:51 AM

Hi,

I have tried it again using the first option and it now works fine.

Thanks for your help!