locked
How do I bind text to a boolean RRS feed

  • Question

  • My code is:

    private static boolean isInt(String s)
     {
         try{
             Integer.parseInt(s);
             return true;
            }
            catch(NumberFormatException e){
                return false;
            }

     

    Is it possible to make it say something like "when X  is not an integer write: X has to be added as numbers" or something like that?

    Wednesday, May 11, 2011 6:03 PM

Answers

  • What are you trying to do?  Are you wanting to data bind some text to one of two messages, depending on whether something represents a number or not?  You could just implement a method on your object that returns the desired text and then bind to that.

    In a broader context, if you're trying to tell a user that they've entered text, where a number is expected, you can leverage support for data validation in WPF.  You can use an ExceptionValidationRule to catch exceptions that occur when you try to assign a text value to a numeric property.

    See http://blogs.msdn.com/b/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx a good explanation of data validation.

    • Proposed as answer by Min Zhu Friday, May 13, 2011 9:53 AM
    • Marked as answer by Min Zhu Wednesday, May 18, 2011 3:02 AM
    Wednesday, May 11, 2011 6:29 PM
  • Hi MrsNoob,

    I think Sean's suggestion is a prefered way to accomplish this.

    And I think an IValueConverter is the answer to your original question. It's possible to use converter to converte a bool value to different string in a binding. See here for more information.

    If you still have any questions or concerns about this issue, please feel free to let me know.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Wednesday, May 18, 2011 3:01 AM
    Friday, May 13, 2011 9:56 AM

All replies

  • What are you trying to do?  Are you wanting to data bind some text to one of two messages, depending on whether something represents a number or not?  You could just implement a method on your object that returns the desired text and then bind to that.

    In a broader context, if you're trying to tell a user that they've entered text, where a number is expected, you can leverage support for data validation in WPF.  You can use an ExceptionValidationRule to catch exceptions that occur when you try to assign a text value to a numeric property.

    See http://blogs.msdn.com/b/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx a good explanation of data validation.

    • Proposed as answer by Min Zhu Friday, May 13, 2011 9:53 AM
    • Marked as answer by Min Zhu Wednesday, May 18, 2011 3:02 AM
    Wednesday, May 11, 2011 6:29 PM
  • I don't understand what are you trying to do 
    Wednesday, May 11, 2011 10:43 PM
  • Hi MrsNoob,

    I think Sean's suggestion is a prefered way to accomplish this.

    And I think an IValueConverter is the answer to your original question. It's possible to use converter to converte a bool value to different string in a binding. See here for more information.

    If you still have any questions or concerns about this issue, please feel free to let me know.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Wednesday, May 18, 2011 3:01 AM
    Friday, May 13, 2011 9:56 AM