Untitled


SUBMITTED BY: Guest

DATE: Dec. 11, 2013, 6:07 a.m.

FORMAT: Text only

SIZE: 2.2 kB

HITS: 782

  1. (99)999-9999
  2. (__)___-____
  3. public class MaskVisibilityBehavior : Behavior<MaskedTextBox> {
  4. private FrameworkElement _contentPresenter;
  5. protected override void OnAttached() {
  6. base.OnAttached();
  7. AssociatedObject.Loaded += (sender, args) => {
  8. _contentPresenter = AssociatedObject.Template.FindName("PART_ContentHost", AssociatedObject) as FrameworkElement;
  9. if (_contentPresenter == null)
  10. throw new InvalidCastException();
  11. AssociatedObject.TextChanged += OnTextChanged;
  12. AssociatedObject.GotFocus += OnGotFocus;
  13. AssociatedObject.LostFocus += OnLostFocus;
  14. UpdateMaskVisibility();
  15. };
  16. }
  17. protected override void OnDetaching() {
  18. AssociatedObject.TextChanged -= OnTextChanged;
  19. AssociatedObject.GotFocus -= OnGotFocus;
  20. AssociatedObject.LostFocus -= OnLostFocus;
  21. base.OnDetaching();
  22. }
  23. private void OnLostFocus(object sender, RoutedEventArgs routedEventArgs) {
  24. UpdateMaskVisibility();
  25. }
  26. private void OnGotFocus(object sender, RoutedEventArgs routedEventArgs) {
  27. UpdateMaskVisibility();
  28. }
  29. private void OnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs) {
  30. UpdateMaskVisibility();
  31. }
  32. private void UpdateMaskVisibility() {
  33. _contentPresenter.Visibility = AssociatedObject.MaskedTextProvider.AssignedEditPositionCount > 0 ||
  34. AssociatedObject.IsFocused
  35. ? Visibility.Visible
  36. : Visibility.Hidden;
  37. }
  38. }
  39. <xctk:MaskedTextBox Mask="(000) 000-0000"
  40. ValueDataType="{x:Type s:String}">
  41. <i:Interaction.Behaviors>
  42. <local:MaskVisibilityBehavior />
  43. </i:Interaction.Behaviors>
  44. </xctk:MaskedTextBox>

comments powered by Disqus