This project is read-only.
1
Vote

Multi-culture support

description

The control currently assumes that all numbers are formatted using the invariant culture (en-US), with positive numbers formatted as "$n" and negative numbers formatted as "($n)".

To properly support other cultures, the control should take into account the CurrencyPositivePattern and CurrencyNegativePattern properties of the NumberFormatInfo for the ConverterCulture associated with the parent binding.

The control should also work with cultures where the currency symbol or negative symbol are not a single character:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
   var tb = sender as CurrencyTextBox;
   if (tb == null) return;

   int index = tb.Text.Length;
   if (index == 0) return;

   var binding = tb.GetBindingExpression(TextProperty);
   if (binding != null && binding.ParentBinding.StringFormat == "C")
   {
      var culture = binding.ParentBinding.ConverterCulture ?? System.Globalization.CultureInfo.InvariantCulture;
      if (Number < 0)
      {
         switch (culture.NumberFormat.CurrencyNegativePattern)
         {
            case 0: // ($n)
            case 14: // ($ n)
            {
               index -= 1;
               break;
            }
            case 3: // $n-
            case 11: // $ n-
            {
               index -= culture.NumberFormat.NegativeSign.Length;
               break;
            }
            case 5: // -n$
            {
               index -= culture.NumberFormat.CurrencySymbol.Length;
               break;
            }
            case 4: // (n$)
            case 8: // -n $
            {
               index -= 1 + culture.NumberFormat.CurrencySymbol.Length;
               break;
            }
            case 6: // n-$
            case 7: // n$-
            {
               index -= culture.NumberFormat.CurrencySymbol.Length + culture.NumberFormat.NegativeSign.Length;
               break;
            }
            case 15: // (n $)
            {
               index -= 2 + culture.NumberFormat.CurrencySymbol.Length;
               break;
            }
            case 10: // n $-
            case 13: // n- $
            {
               index -= 1 + culture.NumberFormat.CurrencySymbol.Length + culture.NumberFormat.NegativeSign.Length;
               break;
            }
            case 1: // -$n
            case 2: // $-n
            case 9: // -$ n
            case 12: // $ -n
            {
               break;
            }
            default:
            {
               System.Diagnostics.Trace.TraceWarning("Unknown currency negative pattern: {0:D}", culture.NumberFormat.CurrencyNegativePattern);
               break;
            }
         }
      }
      else
      {
         switch (culture.NumberFormat.CurrencyPositivePattern)
         {
            case 0: // $n
            case 2: // $ n
            {
               break;
            }
            case 1: // n$
            {
               index -= culture.NumberFormat.CurrencySymbol.Length;
               break;
            }
            case 3: // n $
            {
               index -= 1 + culture.NumberFormat.CurrencySymbol.Length;
               break;
            }
            default:
            {
               System.Diagnostics.Trace.TraceWarning("Unknown currency positive pattern: {0:D}", culture.NumberFormat.CurrencyPositivePattern);
               break;
            }
         }
      }
   }

   if (index < 0) index = 0;
   tb.CaretIndex = index;
}

comments