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. Issue with binding and ValidationRules

Issue with binding and ValidationRules

Scheduled Pinned Locked Moved WPF
wpfhelpcsswcfquestion
6 Posts 2 Posters 0 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.
  • R Offline
    R Offline
    Rohde
    wrote on last edited by
    #1

    I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):

    <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
    <Label>Factor:</Label>
    <TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
    <TextBox.Text>
    <Binding Path="." UpdateSourceTrigger="PropertyChanged">
    <Binding.ValidationRules>
    <validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
    </Binding.ValidationRules>
    </Binding>
    </TextBox.Text>
    </TextBox>
    </StackPanel>

    This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):

    public int ZoomFactor
    {
    get
    {
    return (int) this.DataContext;
    }

            set
            {
                this.DataContext = value;
            }
        }
    

    As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?


    "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
    -Atlas Shrugged, Ayn Rand

    L 2 Replies Last reply
    0
    • R Rohde

      I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):

      <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
      <Label>Factor:</Label>
      <TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
      <TextBox.Text>
      <Binding Path="." UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
      <validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
      </Binding.ValidationRules>
      </Binding>
      </TextBox.Text>
      </TextBox>
      </StackPanel>

      This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):

      public int ZoomFactor
      {
      get
      {
      return (int) this.DataContext;
      }

              set
              {
                  this.DataContext = value;
              }
          }
      

      As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?


      "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
      -Atlas Shrugged, Ayn Rand

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

      Give this short article a read. Your missing the NotifyOnValidationError property setting. http://blog.spencen.com/2007/10/04/wpf-validation-rules.aspx[^]

      Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

      Just a grain of sand on the worlds beaches.

      R 1 Reply Last reply
      0
      • L Lost User

        Give this short article a read. Your missing the NotifyOnValidationError property setting. http://blog.spencen.com/2007/10/04/wpf-validation-rules.aspx[^]

        Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

        Just a grain of sand on the worlds beaches.

        R Offline
        R Offline
        Rohde
        wrote on last edited by
        #3

        I did try that earlier (and now again), and the result is the same. Validate() never fires!


        "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
        -Atlas Shrugged, Ayn Rand

        1 Reply Last reply
        0
        • R Rohde

          I have a problem with ValidationRules. Basically the data binding works, but the Validate-method of the ValidationRules never fire. This is part of my XAML (ZoomDialogBox.xaml):

          <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center">
          <Label>Factor:</Label>
          <TextBox Name="FactorTextBox" Width="30" Margin="10,0,0,0">
          <TextBox.Text>
          <Binding Path="." UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
          <validators:ZoomFactorValidationRule MinZoomFactor="1" MaxZoomFactor="20" />
          </Binding.ValidationRules>
          </Binding>
          </TextBox.Text>
          </TextBox>
          </StackPanel>

          This is the property I am binding to, and this binding works (from ZoomDialogBox.cs):

          public int ZoomFactor
          {
          get
          {
          return (int) this.DataContext;
          }

                  set
                  {
                      this.DataContext = value;
                  }
              }
          

          As said, the binding works, but the ValidationRules is never applied. What am I doing wrong here?


          "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
          -Atlas Shrugged, Ayn Rand

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

          Your ZoomFactor property should be turned into a property with a backing field. There are many different ways for the TextBox to bind to the ZoomFactor property. One way is in the constructor of the ZoomDialogBox.cs file, use this line of code this.DataContext = this; Then change the Path in the TextBox to ZoomFactor. You should also look at the VS Output window to see any data binding errors when your application is running.

          Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

          Just a grain of sand on the worlds beaches.

          R 1 Reply Last reply
          0
          • L Lost User

            Your ZoomFactor property should be turned into a property with a backing field. There are many different ways for the TextBox to bind to the ZoomFactor property. One way is in the constructor of the ZoomDialogBox.cs file, use this line of code this.DataContext = this; Then change the Path in the TextBox to ZoomFactor. You should also look at the VS Output window to see any data binding errors when your application is running.

            Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

            Just a grain of sand on the worlds beaches.

            R Offline
            R Offline
            Rohde
            wrote on last edited by
            #5

            Yes, yes yes! Thank you. Now it works. :):) And thanks for the tip on the Output window, I did not know that databinding errors was shown there. That can be a great time-saver. Once again, thanks for the help.


            "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
            -Atlas Shrugged, Ayn Rand

            L 1 Reply Last reply
            0
            • R Rohde

              Yes, yes yes! Thank you. Now it works. :):) And thanks for the tip on the Output window, I did not know that databinding errors was shown there. That can be a great time-saver. Once again, thanks for the help.


              "When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
              -Atlas Shrugged, Ayn Rand

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

              Glad you're up and running. Have a great day!

              Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

              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