I am using the following to selectAll text when the XamCurrencyEditor gets focus, but the cursor is defaulted to the "cents" part of the editor. This results in the SpinButtons adjusting the cents portion of the amount, but i would like it to default to adjust the $ portion instead. When a user focuses the editor with $10.00 displayed, and clicks "UP", I want it to become $11.00, not $10.01. How can I accomplish this?
In XamCurrencyEditor's XAML: GotFocus="XamCurrencyEditorGotFocus"
code-behind:
private void XamCurrencyEditorGotFocus(object sender, RoutedEventArgs e) { XamCurrencyEditor currencyEditor = sender as XamCurrencyEditor; if (currencyEditor != null) { if (!String.IsNullOrEmpty(currencyEditor.Text)) { currencyEditor.SelectAll(); } } }
Hello Travis,
You can try to use the SelectionStart property. You can add this line of code below SelectAll() method:
currencyEditor.SelectionStart = 0;
Hope this will help you.
Regards,
Anastas
That doesn't work for me, as it defeats the purpose of selecting the text to begin with, the user can't just start typing. ie. $750.00, get focus, type 500, get $500,750.00! I thought about using SelectionStart to something like the length minus the cents portion, but that still fouls it up, as the user then tries to type (starting with $750.00) 500.23 and will get $50023.00!! I think I need to somehow make the Spin Increment $1.00 UNLESS the cursor is in the "cents" part, then make it $0.01. That way, if the entire amount is selected, it will replace correctly and get expected results. How to set the Spin Increment when cursor is in "cents" position though?
The default editor's behaviour is to increase the number part with 1 when the cursor/caret is at the number part. And increase the decimal part with 0.[n digits]1 when cursor/caret is at the decimal part.
What you can do in order to change the SpinIncrement when cursor/caret position is changed, is to set some event handlers for PreviewKeyDown and MouseLeftButtonUp/Down events. Check the cursor/caret's position and set the SpinIncrement there.
This is good for setting the SpinIncrement to $1.00 when the editor gets focus, if the amount is greater than $1.00. Or $0.01 when its less than $1.00. However, this doesn't now le the user adjust the cents portion at all, since focus is already "Got". I need to change the SpinIcrement specifically when the user has clicked in the cents portion or something I suppose? As i said, I need to Bind the spin increment amount somehow to the position of the cursor in the text, not just set it upon focus.
if (currencyEditor.SelectionStart > 2) { currencyEditor.SpinIncrement = 1; } else { currencyEditor.SpinIncrement = 0.01; }
You can check where is the cursor, using the SelectionStart property. And based on your mask you can determine where is the cursor - at number or at decimal part.
So if your mask is something like "nnn.nn"(or {currency:3.2}), then this code will check the cursor position and based on this will set the SpinIncrement:
if (editor.SelectionStart >= 4) editor.SpinIncrement = 0.01; else editor.SpinIncrement = 1;