Advertisement
FrayxRulez

TextBox with password & placeholder

Mar 2nd, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9.  
  10. namespace Dubsmash.Controls
  11. {
  12. public class PlatformTextBox : TextBox
  13. {
  14. private ContentControl PlaceholderElement;
  15.  
  16. public PlatformTextBox()
  17. {
  18. DefaultStyleKey = typeof(PlatformTextBox);
  19.  
  20. TextChanged += OnTextChanged;
  21. }
  22.  
  23. public override void OnApplyTemplate()
  24. {
  25. PlaceholderElement = (ContentControl)GetTemplateChild("PlaceholderElement");
  26. DeterminePlaceholderElementVisibility();
  27.  
  28. base.OnApplyTemplate();
  29. }
  30.  
  31. private void DeterminePlaceholderElementVisibility()
  32. {
  33. if (string.IsNullOrEmpty(this.Text))
  34. {
  35. PlaceholderElement.Visibility = Visibility.Visible;
  36. }
  37. else
  38. {
  39. PlaceholderElement.Visibility = Visibility.Collapsed;
  40. }
  41. }
  42.  
  43. private void OnTextChanged(object sender, TextChangedEventArgs e)
  44. {
  45. var binding = this.GetBindingExpression(TextBox.TextProperty);
  46. if (binding != null)
  47. binding.UpdateSource();
  48.  
  49. DeterminePlaceholderElementVisibility();
  50. }
  51.  
  52. #region PlaceholderText
  53. public string PlaceholderText
  54. {
  55. get { return (string)GetValue(PlaceholderTextProperty); }
  56. set { SetValue(PlaceholderTextProperty, value); }
  57. }
  58.  
  59. public static readonly DependencyProperty PlaceholderTextProperty =
  60. DependencyProperty.Register("PlaceholderText", typeof(string), typeof(PlatformTextBox), new PropertyMetadata(string.Empty));
  61. #endregion
  62.  
  63. #region IsPassword
  64. public bool IsPassword
  65. {
  66. get { return (bool)GetValue(IsPasswordProperty); }
  67. set { SetValue(IsPasswordProperty, value); }
  68. }
  69.  
  70. public static readonly DependencyProperty IsPasswordProperty =
  71. DependencyProperty.Register("IsPassword", typeof(bool), typeof(PlatformTextBox), new PropertyMetadata(false, OnIsPasswordChanged));
  72.  
  73. private static void OnIsPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  74. {
  75. if((bool)e.NewValue)
  76. {
  77. (d as PlatformTextBox).FontFamily = new FontFamily("/Assets/Fonts/password.ttf#Password");
  78. }
  79. else
  80. {
  81. (d as PlatformTextBox).FontFamily = (FontFamily)App.Current.Resources["PhoneFontFamilyNormal"];
  82. }
  83. }
  84. #endregion
  85. }
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. <ResourceDictionary
  98. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  99. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  100. xmlns:local="clr-namespace:Dubsmash.Controls">
  101.  
  102. <Style TargetType="local:PlatformTextBox">
  103. <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
  104. <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
  105. <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
  106. <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
  107. <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
  108. <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
  109. <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
  110. <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
  111. <Setter Property="Padding" Value="2"/>
  112. <Setter Property="Template">
  113. <Setter.Value>
  114. <ControlTemplate TargetType="local:PlatformTextBox">
  115. <Grid Background="Transparent">
  116. <VisualStateManager.VisualStateGroups>
  117. <VisualStateGroup x:Name="CommonStates">
  118. <VisualState x:Name="Normal"/>
  119. <VisualState x:Name="MouseOver"/>
  120. <VisualState x:Name="Disabled">
  121. <Storyboard>
  122. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder">
  123. <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
  124. </ObjectAnimationUsingKeyFrames>
  125. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder">
  126. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
  127. </ObjectAnimationUsingKeyFrames>
  128. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
  129. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
  130. </ObjectAnimationUsingKeyFrames>
  131. </Storyboard>
  132. </VisualState>
  133. <VisualState x:Name="ReadOnly">
  134. <Storyboard>
  135. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="MainBorder">
  136. <DiscreteObjectKeyFrame KeyTime="0">
  137. <DiscreteObjectKeyFrame.Value>
  138. <Visibility>Collapsed</Visibility>
  139. </DiscreteObjectKeyFrame.Value>
  140. </DiscreteObjectKeyFrame>
  141. </ObjectAnimationUsingKeyFrames>
  142. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ReadonlyBorder">
  143. <DiscreteObjectKeyFrame KeyTime="0">
  144. <DiscreteObjectKeyFrame.Value>
  145. <Visibility>Visible</Visibility>
  146. </DiscreteObjectKeyFrame.Value>
  147. </DiscreteObjectKeyFrame>
  148. </ObjectAnimationUsingKeyFrames>
  149. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ReadonlyBorder">
  150. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
  151. </ObjectAnimationUsingKeyFrames>
  152. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ReadonlyBorder">
  153. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
  154. </ObjectAnimationUsingKeyFrames>
  155. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
  156. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>
  157. </ObjectAnimationUsingKeyFrames>
  158. </Storyboard>
  159. </VisualState>
  160. </VisualStateGroup>
  161. <VisualStateGroup x:Name="FocusStates">
  162. <VisualState x:Name="Focused">
  163. <Storyboard>
  164. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder">
  165. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>
  166. </ObjectAnimationUsingKeyFrames>
  167. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder">
  168. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>
  169. </ObjectAnimationUsingKeyFrames>
  170. </Storyboard>
  171. </VisualState>
  172. <VisualState x:Name="Unfocused"/>
  173. </VisualStateGroup>
  174. </VisualStateManager.VisualStateGroups>
  175. <Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}"/>
  176. <Border x:Name="ReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed"/>
  177. <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
  178. <ContentControl x:Name="PlaceholderElement" Content="{TemplateBinding PlaceholderText}" Foreground="{StaticResource PhoneDisabledBrush}" FontFamily="{StaticResource PhoneFontFamilyNormal}" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
  179. </Border>
  180. <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}">
  181. <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
  182. </Border>
  183. </Grid>
  184. </ControlTemplate>
  185. </Setter.Value>
  186. </Setter>
  187. </Style>
  188. </ResourceDictionary>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement