Esempio n. 1
0
        private void SetAmountPercentage(bool isAmount)
        {
            IAdjustmentViewModel d = DataContext as IAdjustmentViewModel;

            if (d != null)
            {
                d.AdjustedAmount = 0.0;
            }

            dollarPanelFilled.Background     = dollarPanel.Background = isAmount ? pressed : normal;
            percentagePanelFilled.Background = percentagePanel.Background = isAmount ? normal : pressed;

            dollarTextFilled.Foreground     = dollarText.Foreground = isAmount ? pressedText : normalText;
            percentageTextFilled.Foreground = percentageText.Foreground = isAmount ? normalText : pressedText;
        }
Esempio n. 2
0
        private void AdjustmentTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            Clipboard.Clear();
            IAdjustmentViewModel viewModel    = DataContext as IAdjustmentViewModel;
            TextBox          txtInput         = sender as TextBox;
            NumberFormatInfo numberFormat     = NumberFormatInfo.CurrentInfo;
            string           decimalSeparator = numberFormat.CurrencyDecimalSeparator;
            string           groupSeparator   = numberFormat.CurrencyGroupSeparator;

            initialValue = txtInput.Text;

            if (!e.Key.isValidNumberKey() && e.Key != VirtualKey.Back &&
                !e.Key.ToString().Equals(Constants.KeyCode190) && !e.Key.ToString().Equals(Constants.KeyCode188))
            {
                e.Handled = true;
            }
            string strValue = initialValue.Replace(groupSeparator, "");

            string[] digit = initialValue.Split(decimalSeparator.ToCharArray());
            double   result;
            bool     isDouble = double.TryParse(strValue, out result);

            if (isDouble && strValue.Length >= 12 && !strValue.Contains(decimalSeparator) &&
                e.Key != VirtualKey.Decimal && !e.Key.ToString().Equals(Constants.KeyCode188) && !e.Key.ToString().Equals(Constants.KeyCode190))
            {
                AssignValue(initialValue);
                e.Handled = true;
            }
            else if ((isDouble && strValue.Contains(decimalSeparator) && (e.Key == VirtualKey.Decimal ||
                                                                          e.Key.ToString().Equals(Constants.KeyCode188) || e.Key.ToString().Equals(Constants.KeyCode190))) || strValue.Length >= 15)
            {
                AssignValue(initialValue);
                e.Handled = true;
                return;
            }
            else if (e.Key.ToString().Equals(Constants.KeyCode190) &&
                     groupSeparator.Equals(".") && !strValue.Contains("."))
            {
                AssignValue(initialValue);
                Focus(initialValue);
                e.Handled = true;
                return;
            }
            else if (e.Key.ToString() == Constants.KeyCode188 && groupSeparator.Equals(","))
            {
                AssignValue(initialValue);
                Focus(initialValue);
                e.Handled = true;
            }
            else if (!isDouble && (e.Key == VirtualKey.Back || e.Key.ToString().Equals(Constants.KeyCode188) ||
                                   e.Key.ToString().Equals(Constants.KeyCode190)))
            {
                AssignValue(initialValue);
                e.Handled = true;
            }
            else if ("0".Equals(initialValue) && (e.Key == VirtualKey.Number0 || e.Key == VirtualKey.NumberPad0))
            {
                AssignValue(initialValue);
                e.Handled = true;
            }
            else if (digit.Length == 2 && !string.IsNullOrWhiteSpace(digit[1]) && digit[1].Length == 2)
            {
                AssignValue(initialValue);
                e.Handled = true;
            }

            if (!String.IsNullOrWhiteSpace(txtInput.Text) && viewModel.AdjustmentType == AdjustmentType.Percentage)
            {
                try
                {
                    double val = Convert.ToDouble(txtInput.Text + GetNumber(e.Key));
                    e.Handled = val > 100;
                    if (e.Key == VirtualKey.Back || (!e.Key.isValidNumberKey() && !e.Key.ToString().Equals(Constants.KeyCode190) &&
                                                     !e.Key.ToString().Equals(Constants.KeyCode188)) || (digit.Length == 2 && digit[1] != null && digit[1].Length == 2))
                    {
                        e.Handled = true;
                    }
                }
                catch (Exception)
                {
                    AssignValue(string.Empty);
                    e.Handled = true;
                }
            }
        }