Results 1 to 2 of 2

Thread: Validation rule not working...

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Validation rule not working...

    Hi there,

    i have been trying to implement a custom validation rule. It doesn't seem to be working though I am not getting any errors. I figured out that I needed this line.
    Code:
    xmlns:local="clr-namespace:validatiinTest"
    Yes - I know I spelled Validation wrong when I created the project.

    Everything compiles but nothing happens when I change the value in the textbox that has the validation rule... I mean in the code, I dropped a breakpoint inside the validate method and it never gets called - what am I missing? Below is the code for the whole thing... As far as the book that I have is concerned that is everything I should need. Of course I am not doing anything or checking it but still Validate() should get called.

    Code:
    <Window x:Class="validatiinTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:validatiinTest"
        Title="Window1" Height="300" Width="300" Background="Aqua">
        
        <Grid>
            <TextBox Height="21" Margin="12,62,0,0" Name="TextBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" >
                <TextBox.Text>
                    <Binding  Path="xxxx" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <local:CheckForInteger />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <TextBox Height="23" Margin="77,0,81,29" Name="textBox2" VerticalAlignment="Bottom" />
        </Grid>
    </Window>
    Code:
    namespace validatiinTest
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
        }
    
        public class CheckForInteger : ValidationRule
        {
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureinfo)
            {
                int theNum = 0;
                if (!int.TryParse(value.ToString(), out theNum))
                    return new ValidationResult(false, "Not a number");
                return new ValidationResult(true, "No Error");
            }
        }
    }
    Thanks!
    Anti DUPLO machine!!!

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Validation rule not working...

    Have a look at Binding Validation Sample from MSDN if it helps.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width