Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Validation for ProperyGrid Property

$
0
0
Hi,

you can use PropertyGrid.EditorDefinitions to define a DataTemplate for property of type "Integer" and then use the Binding.ValidationRules.
<Window.Resources>
      <Style x:Key="ControlInError"
             TargetType="xctk:PropertyGridEditorIntegerUpDown">
         <Style.Triggers>
            <Trigger Property="Validation.HasError"
                     Value="true">
               <Setter Property="ToolTip"
                       Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
            </Trigger>
         </Style.Triggers>
      </Style>
   </Window.Resources>
    <Grid>
      <xctk:PropertyGrid x:Name="_propertyGrid">
         <xctk:PropertyGrid.EditorDefinitions>
            <xctk:EditorTemplateDefinition TargetProperties="{x:Type sys:Int32}">
               <xctk:EditorTemplateDefinition.EditingTemplate>
                  <DataTemplate>
                     <xctk:PropertyGridEditorIntegerUpDown TextAlignment="Left"
                                                           Style="{StaticResource ControlInError}">
                        <xctk:PropertyGridEditorIntegerUpDown.Value>
                           <Binding Path="Value"
                                    UpdateSourceTrigger="PropertyChanged">
                              <Binding.ValidationRules>
                                 <local:IntegerValidationRule />
                              </Binding.ValidationRules>
                           </Binding>
                        </xctk:PropertyGridEditorIntegerUpDown.Value>
                     </xctk:PropertyGridEditorIntegerUpDown>
                  </DataTemplate>
               </xctk:EditorTemplateDefinition.EditingTemplate>
            </xctk:EditorTemplateDefinition>
         </xctk:PropertyGrid.EditorDefinitions>
      </xctk:PropertyGrid>
   </Grid>
public class IntegerValidationRule : ValidationRule
  {
    public override ValidationResult Validate( object value, CultureInfo cultureInfo )
    {
      int intValue;
      bool success = int.TryParse( value.ToString(), out intValue );
      if( success && (intValue < 100) )
        return new ValidationResult( true, null );

      return new ValidationResult( false, "Please enter a valid integer value." );
    }
  }

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>