locked
Limiting a Textbox value to a min and max RRS feed

  • Question

  • I just started learning WPF about a week ago.  I've searched the net and have read about validation rules and stuff, but I'm not sure if that's what I want.
    Here IS what I want:
    I'm creating a custom numeric editor control that contains a textbox and slider among other things.  I've successfully got the textbox and slider bound together.  However, I want to expose a public property for Min and Max for my control.  I want the slider min and max to be bound to these values and I also want it such that the textbox doesn't allow numbers outside the range to be entered.   However, instead of using a ValidationRule to display an error message, I simply want to just clamp to my min/max when an invalid number is entered. 
    I thought about putting the logic for this inside my Value property, but I've read that putting logic in the CLR property of a DependencyProperty is not a good idea.
    Suggestions?

    Friday, June 27, 2008 3:04 PM

Answers

All replies

  • Checking in again for replies.
    Friday, June 27, 2008 5:43 PM
  • Try using a value converter instead of a validation rule.
    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx 
    Regards,
    -Ron
    • Proposed as answer by RNEELY Friday, June 27, 2008 7:11 PM
    • Marked as answer by Marco Zhou Thursday, July 3, 2008 11:13 AM
    Friday, June 27, 2008 5:51 PM
  • What comes to my mind is use 2 way binding to a property you have in your code-behind. When the property changes ("set" is called), check the value. Compare it to the min/max properties and if it's out of range, clamp it programmatically to where it should be.
    Friday, June 27, 2008 6:47 PM
  • Thanks for the reply.  But aren't we supposed to avoid any logic other than setvalue and getvalue in the property setter if it is a DependencyProperty?  
    Saturday, June 28, 2008 4:18 AM