Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. textbox validation

textbox validation

Scheduled Pinned Locked Moved WPF
csharpwpfcomquestion
6 Posts 2 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    angels777
    wrote on last edited by
    #1

    hi.. is there a simple way to validate the textbox where it indicate the graphic beside the textbox to show the message which it is wrong.. i tried to learn from this link http://www.codeproject.com/KB/WPF/Validizor.aspx?display=Print but i fail to do it .. too many form link everywhere..

    L 1 Reply Last reply
    0
    • A angels777

      hi.. is there a simple way to validate the textbox where it indicate the graphic beside the textbox to show the message which it is wrong.. i tried to learn from this link http://www.codeproject.com/KB/WPF/Validizor.aspx?display=Print but i fail to do it .. too many form link everywhere..

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I just posted an article on this. http://www.codeproject.com/KB/WPF/WPFBusinessAppsPartTwo.aspx[^] Download the code and look at the resource dictionaries in the Skins folder. Basically you need to set up a control template. I have placed the following markup at application scope. The markup will render a * to the left of the TextBox is there is a validation error.

      <ControlTemplate x:Key="validationTemplate">
      <DockPanel>
      <TextBlock Margin="5,0,5,0" Foreground="Red" FontSize="16"
      VerticalAlignment="Center" Text="*" />
      <AdornedElementPlaceholder />
      </DockPanel>
      </ControlTemplate>

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

      This should get you going. Please read the article, it will really help in this situation.

      Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your Articles

      Just a grain of sand on the worlds beaches.

      A 1 Reply Last reply
      0
      • L Lost User

        I just posted an article on this. http://www.codeproject.com/KB/WPF/WPFBusinessAppsPartTwo.aspx[^] Download the code and look at the resource dictionaries in the Skins folder. Basically you need to set up a control template. I have placed the following markup at application scope. The markup will render a * to the left of the TextBox is there is a validation error.

        <ControlTemplate x:Key="validationTemplate">
        <DockPanel>
        <TextBlock Margin="5,0,5,0" Foreground="Red" FontSize="16"
        VerticalAlignment="Center" Text="*" />
        <AdornedElementPlaceholder />
        </DockPanel>
        </ControlTemplate>

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

        This should get you going. Please read the article, it will really help in this situation.

        Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your Articles

        Just a grain of sand on the worlds beaches.

        A Offline
        A Offline
        angels777
        wrote on last edited by
        #3

        i am using 2005 .. sorry

        L 1 Reply Last reply
        0
        • A angels777

          i am using 2005 .. sorry

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          What I showed you is still valid (almost). IN WPF 3.0 you still have a Validation.ErrorTemplate. The template I showed will do what you need. The validation error occurs when the object you are binding to throws an exception. Just modify your business object to throw an exception. The below link shows how to do validation in 3.0 and 3.5. Very easy to do. :cool: http://blogs.msdn.com/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx[^]

          Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your Articles

          Just a grain of sand on the worlds beaches.

          A 1 Reply Last reply
          0
          • L Lost User

            What I showed you is still valid (almost). IN WPF 3.0 you still have a Validation.ErrorTemplate. The template I showed will do what you need. The validation error occurs when the object you are binding to throws an exception. Just modify your business object to throw an exception. The below link shows how to do validation in 3.0 and 3.5. Very easy to do. :cool: http://blogs.msdn.com/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx[^]

            Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your Articles

            Just a grain of sand on the worlds beaches.

            A Offline
            A Offline
            angels777
            wrote on last edited by
            #5

            thanks.. great.. but it took about 1 second to display the tooltip message when i hover the mouse over the texbox.. so in my textblock i put a image beside the textbox <TextBlock DockPanel.Dock="Right" Foreground="Orange" FontSize="12pt"> <Image Stretch="None" Source="error.gif" /> </TextBlock> can i display the message when i hover that image instead of hover the textbox?

            L 1 Reply Last reply
            0
            • A angels777

              thanks.. great.. but it took about 1 second to display the tooltip message when i hover the mouse over the texbox.. so in my textblock i put a image beside the textbox <TextBlock DockPanel.Dock="Right" Foreground="Orange" FontSize="12pt"> <Image Stretch="None" Source="error.gif" /> </TextBlock> can i display the message when i hover that image instead of hover the textbox?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Not sure if you can display a ToolTip in the Adorner Layer. You can try and alter the code so that the ToolTip for the above TextBlock, but I'm not sure, I've never tried. There is a property on the ToolTipService that controls how long it waits before displaying the ToolTip. Look into this and change the value.

              Cheers, Karl » CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your Articles

              Just a grain of sand on the worlds beaches.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups