Need validation

Feb 29, 2012 at 5:11 PM

Does Your Property Grid support validation?

Feb 29, 2012 at 5:31 PM
Edited Feb 29, 2012 at 5:41 PM

For validation I implement IDataErrorInfo for PropertyItemValue

and change template for standard values

 

 

<DataTemplate x:Key="{x:Static local:EditorKeys.DefaultEditorKey}">  
 <TextBox
	x:Name="PART_editor"
	Text="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"
	IsReadOnly="{Binding IsReadOnly}"
	local:TextBoxExtender.CommitOnEnter="true"
	local:TextBoxExtender.RollbackOnEscape="true"
	local:TextBoxExtender.CommitOnTyping="false">           

<!-- Next style with trigger to fill background with gray color when value is readonly -->

	<TextBox.Style>               
		<Style TargetType="TextBox">                   
			<Style.Triggers>                       
				<Trigger Property="IsReadOnly" Value="True">                           
					<Setter Property="Background" Value="LightGray" />                       
				</Trigger>                   
			</Style.Triggers>               
		</Style>          
	</TextBox.Style>       
</TextBox>   
</DataTemplate>

 

Feb 29, 2012 at 5:39 PM

And add some code to PropertyItemValue class

      public string this[string columnName]
      {
          get
          {
              if ("Value" == columnName)
              {
                  var component = _property.Component;
                  if (component is IDataErrorInfo)
                  {
                      var dataErrorInfo = (IDataErrorInfo) component;
                      return dataErrorInfo[_property.Name];
                  }
              }

              return string.Empty;
          }
      }

      public string Error
      {
          get { return string.Empty; }
      }