Condividi tramite


Validation Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides methods and attached properties that support data validation and govern the visual state of the control.

Inheritance Hierarchy

System.Object
  System.Windows.Controls.Validation

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public NotInheritable Class Validation
public static class Validation

The Validation type exposes the following members.

Attached Properties

  Name Description
Public attached propertySupported by Silverlight for Windows Phone Errors Gets a collection of ValidationError objects associated with the binding target element.
Public attached propertySupported by Silverlight for Windows Phone HasError Gets a value that indicates whether any binding on the target element has a validation error.

Top

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows Phone GetErrors Gets the value of the Errors attached property for the specified element.
Public methodStatic memberSupported by Silverlight for Windows Phone GetHasError Gets the value of the HasError attached property for the specified element.

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows Phone ErrorsProperty Identifies the Errors attached property.
Public fieldStatic memberSupported by Silverlight for Windows Phone HasErrorProperty Identifies the HasError attached property.

Top

Remarks

The Validation class provides support for displaying validation error information by using a control template. For more information about control templates, see Control Customization.

Examples

The following code example demonstrates how to use the Validation class in a control template. This template is used as a static resource by several default control templates to display a validation error message in a tool tip. For a complete control template example, see TextBox Styles and Templates.

<ControlTemplate x:Key="ValidationToolTipTemplate">
      <Grid x:Name="Root" Margin="5,0" RenderTransformOrigin="0,0" Opacity="0">
          <Grid.RenderTransform>
              <TranslateTransform x:Name="xform" X="-25"/>
          </Grid.RenderTransform>
          <vsm:VisualStateManager.VisualStateGroups>
              <vsm:VisualStateGroup Name="OpenStates">
                  <vsm:VisualStateGroup.Transitions>
                      <vsm:VisualTransition GeneratedDuration="0"/>
                      <vsm:VisualTransition To="Open" GeneratedDuration="0:0:0.2">
                          <Storyboard>
                              <DoubleAnimation Storyboard.TargetName="xform" Storyboard.TargetProperty="X" To="0" Duration="0:0:0.2">
                                  <DoubleAnimation.EasingFunction>
                                      <BackEase Amplitude=".3" EasingMode="EaseOut"/>
                                  </DoubleAnimation.EasingFunction>
                              </DoubleAnimation>
                              <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2"/>
                          </Storyboard>
                      </vsm:VisualTransition>
                  </vsm:VisualStateGroup.Transitions>
                  <vsm:VisualState x:Name="Closed">
                      <Storyboard>
                          <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/>
                      </Storyboard>
                  </vsm:VisualState>
                  <vsm:VisualState x:Name="Open">
                      <Storyboard>
                          <DoubleAnimation Storyboard.TargetName="xform" Storyboard.TargetProperty="X" To="0" Duration="0"/>
                          <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
                      </Storyboard>
                  </vsm:VisualState>
              </vsm:VisualStateGroup>
          </vsm:VisualStateManager.VisualStateGroups>

          <Border Margin="4,4,-4,-4" Background="#052A2E31" CornerRadius="5"/>
          <Border Margin="3,3,-3,-3" Background="#152A2E31" CornerRadius="4"/>
          <Border Margin="2,2,-2,-2" Background="#252A2E31" CornerRadius="3"/>
          <Border Margin="1,1,-1,-1" Background="#352A2E31" CornerRadius="2"/>

          <Border Background="#FFDC000C" CornerRadius="2"/>
          <Border CornerRadius="2">
              <TextBlock 
                  UseLayoutRounding="false" 
                  Foreground="White" Margin="8,4,8,4" MaxWidth="250" TextWrapping="Wrap" Text="{Binding (Validation.Errors)[0].ErrorContent}"/>
          </Border>
      </Grid>
  </ControlTemplate>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Other Resources