textbox validation
-
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..
-
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..
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 ArticlesJust a grain of sand on the worlds beaches.
-
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 ArticlesJust a grain of sand on the worlds beaches.
-
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 ArticlesJust a grain of sand on the worlds beaches.
-
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 ArticlesJust a grain of sand on the worlds beaches.
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?
-
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?
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 ArticlesJust a grain of sand on the worlds beaches.