Don't make any difference between hex or decimal. Your figures look the same. Why do you need '0x' prefix? This is not \a part of hexadecimal notation, this is just a usual marker for the literal is some programming language. Why would you need it in UI, especially in the range (0 to 7)?
Better do the following: filter out all characters except 0 to 7 and backspace (0x8) from input handling the event
PreviewTextInput
. For the technique of filtering out of the input, please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewtextinput.aspx[
^],
http://msdn.microsoft.com/en-us/library/ms752279.aspx[
^].
When you need to use the entered value, parse the string
byte.TryParse
. In case of failure (the call returns false) fail the validation as a wrong numeric format (in practice, when you use filtering, it could only happen if the user inputs too many characters; you can limit the length (even by 1 character), using
System.Windows.Controls.TextBox.MaxLength
, then you won't have this problem at all).
On next step, validate that the returned value is in the valid range. Again, with the range (0 to 7), filtering and limited length you won't have this problem.
Practically, for this particular range, you won't need any validation (surprise!).
Alternatively, why would you ever need text box and validation mess for small ranges? You would be much better off with the Numeric Spinner Control. Please see:
http://code.google.com/p/phoenix-control-library/[
^].
Good luck,
—SA