Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. /*
  2. * Copyright & License: :) The Code is very Trivial. If copyright applicable, then (C) 2015 Bsod64 :D
  3. *
  4. * Usage:
  5. * Add it to your project.
  6. *
  7. * In your xaml file first add to the xmlns section something along the lines of:
  8. * xmlns:customwidgets="clr-namespace:Bsod64.CustomWpfControls"
  9. * (To change this, simple reflect your change in the namespace of this file.)
  10. *
  11. * Then use it in your xaml file:
  12. * <customwidgets:TextboxWithPlaceholder Placeholder="My placeholder text here" />
  13. *
  14. */
  15.  
  16. using Microsoft.Win32;
  17. using System;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Media;
  21.  
  22. namespace Bsod64.CustomWpfControls
  23. {
  24. class TextboxWithPlaceholder: TextBox
  25. {
  26. private Brush _placeholderColor = Brushes.Gray;
  27.  
  28. public string Placeholder
  29. {
  30. get
  31. {
  32. return (string)GetValue(PlaceholderProperty);
  33. }
  34. set
  35. {
  36. SetValue(PlaceholderProperty, value);
  37. }
  38. }
  39.  
  40. public static readonly DependencyProperty PlaceholderProperty =
  41. DependencyProperty.Register("Placeholder", typeof(string), typeof(TextboxWithPlaceholder), new PropertyMetadata(default(string)));
  42.  
  43.  
  44.  
  45. protected void Usability_PropertyChanged(object sender, EventArgs e)
  46. {
  47. if(!SystemParameters.HighContrast)
  48. {
  49. _placeholderColor = Brushes.Gray;
  50. if(Text.Equals(Placeholder))
  51. Foreground = _placeholderColor;
  52. }
  53. else
  54. {
  55. ClearValue(TextboxWithPlaceholder.ForegroundProperty);
  56. _placeholderColor = Foreground;
  57. }
  58. }
  59.  
  60. protected override void OnInitialized(EventArgs e)
  61. {
  62. base.OnInitialized(e);
  63. SystemEvents.UserPreferenceChanged += Usability_PropertyChanged; // Starting .NET 4.5 can you can use SystemParameters.StaticPropertyChanged
  64. ShowPlaceholder();
  65. }
  66.  
  67. protected override void OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs e)
  68. {
  69. base.OnGotKeyboardFocus(e);
  70. if(Text.Equals(Placeholder))
  71. HidePlaceholder();
  72. }
  73.  
  74. protected override void OnLostKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs e)
  75. {
  76. base.OnLostKeyboardFocus(e);
  77. if(Text.Equals(String.Empty))
  78. ShowPlaceholder();
  79. }
  80.  
  81. public void ShowPlaceholder()
  82. {
  83. Text = Placeholder;
  84. Foreground = _placeholderColor;
  85. FontStyle = FontStyles.Italic;
  86. }
  87.  
  88. public void HidePlaceholder()
  89. {
  90. Text = String.Empty;
  91. ClearValue(TextboxWithPlaceholder.ForegroundProperty);
  92. FontStyle = FontStyles.Normal;
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement