Click here to Skip to main content
15,891,253 members
Articles / Desktop Programming / WPF

WPF.JoshSmith

Rate me:
Please Sign up or sign in to vote.
4.99/5 (51 votes)
13 Jul 2008CPOL5 min read 390.7K   4.8K   263  
A free library of controls and utility classes for use in WPF applications.
<Window x:Class="Test.RegexValidator.RegexValidatorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:jas="clr-namespace:WPF.JoshSmith.Controls.Validation;assembly=WPF.JoshSmith" 
    xmlns:local="clr-namespace:Test.RegexValidator" 
    Title="RegexValidator &amp; RegexValidationRule Test" Height="300" Width="375"
    FontSize="12"
    >
  <Window.Resources>
    <!-- This style allows a validation error message to be 
         displayed in a TextBox's tooltip. -->
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Margin" Value="0,8" />
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
          <Setter Property="ToolTip"
            Value="{Binding 
                    RelativeSource={RelativeSource Self}, 
                    Path=(Validation.Errors)[0].ErrorContent
                   }"/>
        </Trigger>
      </Style.Triggers>      
    </Style>
  </Window.Resources>
  
  <StackPanel Margin="4" VerticalAlignment="Center">
    <TextBlock>E-mail Address:</TextBlock>
    <TextBox 
      Text="{Binding Path=EmailAddress, UpdateSourceTrigger=PropertyChanged}" 
      jas:RegexValidator.RegexText="^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$" 
      jas:RegexValidator.ErrorMessage="Invalid e-mail address." 
      />

    <!-- Sets the RegexText attached property to a static property
         which returns a localized regex, for the current date format. -->
    <TextBlock>Localized Date:</TextBlock>
    <TextBox 
      Text="{Binding Path=DateString, UpdateSourceTrigger=PropertyChanged}" 
      jas:RegexValidator.RegexText="{x:Static local:DateFormatRegex.DateRegex}" 
      jas:RegexValidator.ErrorMessage="Invalid date format." 
      />

    <!-- Explicitly creates a RegexValidationRule in order to set the RegexOptions. -->
    <TextBlock>Product Code:</TextBlock>
    <TextBox>
      <TextBox.Text>
        <Binding Path="ProductCode" UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
            <jas:RegexValidationRule
              RegexText="^[A-Z]{3}\.[0-9]{3}$"
              ErrorMessage="Invalid product code.  (Examples: ABC.123  xyz.789)"
              RegexOptions="IgnoreCase"
              />
              </Binding.ValidationRules>
            </Binding>
      </TextBox.Text>
    </TextBox>    
  </StackPanel>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions