Advertisement
Guest User

Untitled

a guest
Sep 1st, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. ВАЖНО UpdateSourceTrigger=EXPLICIT
  2. пришлось использовать ValidationRule если нужен красный бордер на 3,
  3. просто установить цвет бесполезно - его перекрывает триггер контрола, а делать свой шаблон нах надо
  4.  
  5. public class DoubleTextBox : TextBox
  6. {
  7. private BindingExpression _be = null!;
  8. private readonly bool _designMode;
  9.  
  10. public DoubleTextBox()
  11. {
  12. TextChanged += OnTextChanged;
  13. DataContextChanged += OnDataContextChanged;
  14. _designMode = DesignerProperties.GetIsInDesignMode(this);
  15. }
  16.  
  17. private void OnTextChanged(object sender, TextChangedEventArgs e)
  18. {
  19. if (!string.IsNullOrWhiteSpace(Text))
  20. {
  21. if (Text.EndsWith(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
  22. {
  23. if (_be! is { } be)
  24. {
  25. be.ValidateWithoutUpdate();
  26. }
  27. return;
  28. }
  29.  
  30. if (!IsInputNumeric(Text, CultureInfo.CurrentCulture))
  31. {
  32. Text = string.Empty;
  33. return;
  34. }
  35. }
  36.  
  37. if (!_designMode)
  38. _be.UpdateSource();
  39. }
  40.  
  41. [MemberNotNull(nameof(_be))]
  42. private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  43. {
  44. _be = GetBindingExpression(TextProperty)!;
  45. }
  46.  
  47.  
  48. #region Overrides of TextBoxBase
  49.  
  50. /// <inheritdoc />
  51. protected override void OnTextInput(TextCompositionEventArgs e)
  52. {
  53. var culture = CultureInfo.CurrentCulture;
  54. if (TryHandleSpecialNonNumericCharacter(e, culture))
  55. {
  56. base.OnTextInput(e);
  57. return;
  58. }
  59.  
  60. // Input is not a special character.
  61. // Cancel text input if non-numeric.
  62. e.Handled = !IsInputNumeric(e.Text, culture);
  63.  
  64. base.OnTextInput(e);
  65. }
  66.  
  67. private bool IsInputNumeric(string input, IFormatProvider culture) =>
  68. double.TryParse(input, NumberStyles.Number, culture, out _);
  69.  
  70. private bool TryHandleSpecialNonNumericCharacter(TextCompositionEventArgs inputArgs, CultureInfo culture)
  71. {
  72. var input = inputArgs.Text;
  73. switch (input)
  74. {
  75. case var _ when input.Equals(culture.NumberFormat.NegativeSign,
  76. StringComparison.CurrentCultureIgnoreCase):
  77. case var _ when input.Equals(culture.NumberFormat.PositiveSign,
  78. StringComparison.CurrentCultureIgnoreCase):
  79. break;
  80.  
  81. // Allow single decimal separator
  82. case var _ when input.Equals(culture.NumberFormat.NumberDecimalSeparator,
  83. StringComparison.CurrentCultureIgnoreCase):
  84. inputArgs.Handled = Text.Contains(culture.NumberFormat.NumberDecimalSeparator);
  85. break;
  86. default:
  87. return false;
  88. }
  89.  
  90. return true;
  91. }
  92.  
  93. #endregion
  94. }
  95.  
  96. --------------------------
  97.  
  98. public class FullDoubleNumberhValidationRule : ValidationRule
  99. {
  100. private readonly ValidationResult _validResult = new(true, null);
  101. private readonly ValidationResult _errorResult = new(false, string.Empty);
  102.  
  103. public override ValidationResult Validate(object? value, CultureInfo cultureInfo)
  104. {
  105. if (value is string text)
  106. {
  107. if (text.EndsWith(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
  108. return _errorResult;
  109. }
  110. return _validResult;
  111.  
  112. }
  113. }
  114.  
  115. --------------------------
  116.  
  117. <controls:DoubleTextBox d:Text="100">
  118. <controls:DoubleTextBox.Text>
  119. <Binding
  120. Converter="{StaticResource SpentConverter}"
  121. Mode="TwoWay"
  122. Path="Spent"
  123. UpdateSourceTrigger="Explicit">
  124. <Binding.ValidationRules>
  125. <controls:FullDoubleNumberhValidationRule />
  126. </Binding.ValidationRules>
  127. </Binding>
  128. </controls:DoubleTextBox.Text>
  129. </controls:DoubleTextBox>
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement