PasswordBox in Windows phone 8.1 has a property called "IsPasswordRevealButtonEnabled " which helps to show password by clicking the check box underneath it.

  1. <Grid>
  2. <PasswordBox Background="White"
  3. BorderBrush="Gray"
  4. Foreground="Black"
  5. Header="Password"
  6. IsPasswordRevealButtonEnabled="True" />
  7. </Grid>


But we can remove checkbox and place toggle button inside passwordbox which looks much better.

Here is the code for CustomPasswordBox,

  1. <Grid>
  2. <PasswordBox Background="White"
  3. BorderBrush="Gray"
  4. Foreground="Black"
  5. Header="Password"
  6. IsPasswordRevealButtonEnabled="True"
  7. Style="{StaticResource CustomPasswordBoxStyle}" />
  8. </Grid>
  1. <Page.Resources>
  2. <Thickness x:Key="PhoneButtonContentPadding">9.5,0,9.5,3.5</Thickness>
  3. <Style x:Key="CustomToggleButtonStyle" TargetType="ToggleButton">
  4. <Setter Property="Background" Value="Transparent"/>
  5. <Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
  6. <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
  7. <Setter Property="FontWeight" Value="{ThemeResource PhoneButtonFontWeight}"/>
  8. <Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}"/>
  9. <Setter Property="Padding" Value="{ThemeResource PhoneButtonContentPadding}"/>
  10. <Setter Property="MinHeight" Value="{ThemeResource PhoneButtonMinHeight}"/>
  11. <Setter Property="MinWidth" Value="{ThemeResource PhoneButtonMinWidth}"/>
  12. <Setter Property="HorizontalAlignment" Value="Left"/>
  13. <Setter Property="VerticalAlignment" Value="Center"/>
  14. <Setter Property="Template">
  15. <Setter.Value>
  16. <ControlTemplate TargetType="ToggleButton">
  17. <Grid HorizontalAlignment="Center" Background="Transparent">
  18. <VisualStateManager.VisualStateGroups>
  19. <VisualStateGroup x:Name="CommonStates">
  20. <VisualState x:Name="Normal" />
  21. <VisualState x:Name="PointerOver" />
  22. <VisualState x:Name="Pressed" />
  23. <VisualState x:Name="Disabled">
  24. <Storyboard>
  25. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBackground" Storyboard.TargetProperty="Visibility">
  26. <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
  27. </ObjectAnimationUsingKeyFrames>
  28. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="Visibility">
  29. <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
  30. </ObjectAnimationUsingKeyFrames>
  31. </Storyboard>
  32. </VisualState>
  33. <VisualState x:Name="Checked">
  34. <Storyboard>
  35. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="Background">
  36. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneDisabledBrush}" />
  37. </ObjectAnimationUsingKeyFrames>
  38. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="BorderBrush">
  39. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneDisabledBrush}" />
  40. </ObjectAnimationUsingKeyFrames>
  41. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledContent" Storyboard.TargetProperty="Foreground">
  42. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneBackgroundBrush}" />
  43. </ObjectAnimationUsingKeyFrames>
  44. <ColorAnimation Duration="0"
  45. Storyboard.TargetName="EnabledContent"
  46. Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)"
  47. To="#00BAF2"
  48. d:IsOptimized="True" />
  49. </Storyboard>
  50. </VisualState>
  51. <VisualState x:Name="CheckedPointerOver">
  52. <Storyboard>
  53. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="Background">
  54. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneDisabledBrush}" />
  55. </ObjectAnimationUsingKeyFrames>
  56. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="BorderBrush">
  57. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneDisabledBrush}" />
  58. </ObjectAnimationUsingKeyFrames>
  59. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledContent" Storyboard.TargetProperty="Foreground">
  60. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PhoneBackgroundBrush}" />
  61. </ObjectAnimationUsingKeyFrames>
  62. </Storyboard>
  63. </VisualState>
  64. <VisualState x:Name="CheckedPressed" />
  65. <VisualState x:Name="CheckedDisabled">
  66. <Storyboard>
  67. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBackground" Storyboard.TargetProperty="Opacity">
  68. <DiscreteObjectKeyFrame KeyTime="0" Value="0.4" />
  69. </ObjectAnimationUsingKeyFrames>
  70. </Storyboard>
  71. </VisualState>
  72. <VisualState x:Name="Indeterminate" />
  73. <VisualState x:Name="IndeterminatePointerOver" />
  74. <VisualState x:Name="IndeterminatePressed" />
  75. <VisualState x:Name="IndeterminateDisabled">
  76. <Storyboard>
  77. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBackground" Storyboard.TargetProperty="Visibility">
  78. <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
  79. </ObjectAnimationUsingKeyFrames>
  80. <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledBackground" Storyboard.TargetProperty="Visibility">
  81. <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
  82. </ObjectAnimationUsingKeyFrames>
  83. </Storyboard>
  84. </VisualState>
  85. </VisualStateGroup>
  86. </VisualStateManager.VisualStateGroups>
  87. <Border x:Name="EnabledBackground"
  88. Margin="{ThemeResource PhoneTouchTargetOverhang}">
  89. <ContentPresenter x:Name="EnabledContent"
  90. Margin="{TemplateBinding Padding}"
  91. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  92. VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
  93. AutomationProperties.AccessibilityView="Raw"
  94. Content="{TemplateBinding Content}"
  95. ContentTemplate="{TemplateBinding ContentTemplate}" />
  96. </Border>
  97. <Border x:Name="DisabledBackground"
  98. Margin="{ThemeResource PhoneTouchTargetOverhang}"
  99. Background="Transparent"
  100. IsHitTestVisible="False"
  101. Visibility="Collapsed">
  102. <ContentPresenter x:Name="DisabledContent"
  103. Margin="{TemplateBinding Padding}"
  104. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  105. VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
  106. AutomationProperties.AccessibilityView="Raw"
  107. Content="{TemplateBinding Content}"
  108. ContentTemplate="{TemplateBinding ContentTemplate}"
  109. Foreground="{ThemeResource ButtonDisabledForegroundThemeBrush}" />
  110. </Border>
  111. </Grid>
  112. </ControlTemplate>
  113. </Setter.Value>
  114. </Setter>
  115. </Style>
  116. <Style x:Key="CustomPasswordBoxStyle" TargetType="PasswordBox">
  117. <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
  118. <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/>
  119. <Setter Property="Height" Value="67"/>
  120. <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}"/>
  121. <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}"/>
  122. <Setter Property="Background" Value="{ThemeResource TextBoxBackgroundThemeBrush}"/>
  123. <Setter Property="BorderBrush" Value="{ThemeResource TextBoxBorderThemeBrush}"/>
  124. <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}"/>
  125. <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
  126. <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}"/>
  127. <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Auto"/>
  128. <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto"/>
  129. <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
  130. <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
  131. <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
  132. <Setter Property="Margin" Value="{ThemeResource TextControlMarginThemeThickness}"/>
  133. <Setter Property="VerticalAlignment" Value="Top"/>
  134. <Setter Property="Template">
  135. <Setter.Value>
  136. <ControlTemplate TargetType="PasswordBox">
  137. <Grid Background="Transparent">
  138. <Grid.RowDefinitions>
  139. <RowDefinition Height="Auto"/>
  140. <RowDefinition Height="*"/>
  141. <RowDefinition Height="Auto"/>
  142. </Grid.RowDefinitions>
  143. <VisualStateManager.VisualStateGroups>
  144. <VisualStateGroup x:Name="CommonStates">
  145. <VisualState x:Name="Disabled">
  146. <Storyboard>
  147. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement">
  148. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBackgroundThemeBrush}"/>
  149. </ObjectAnimationUsingKeyFrames>
  150. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
  151. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBorderThemeBrush}"/>
  152. </ObjectAnimationUsingKeyFrames>
  153. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
  154. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/>
  155. </ObjectAnimationUsingKeyFrames>
  156. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter">
  157. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/>
  158. </ObjectAnimationUsingKeyFrames>
  159. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="HeaderContentPresenter">
  160. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledHeaderForegroundThemeBrush}"/>
  161. </ObjectAnimationUsingKeyFrames>
  162. </Storyboard>
  163. </VisualState>
  164. <VisualState x:Name="Normal">
  165. <Storyboard>
  166. <DoubleAnimation Duration="0" To="{ThemeResource TextControlBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
  167. </Storyboard>
  168. </VisualState>
  169. <VisualState x:Name="PointerOver"/>
  170. <VisualState x:Name="Focused">
  171. <Storyboard>
  172. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
  173. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}"/>
  174. </ObjectAnimationUsingKeyFrames>
  175. <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PlaceholderTextContentPresenter"/>
  176. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement">
  177. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxFocusedBackgroundThemeBrush}"/>
  178. </ObjectAnimationUsingKeyFrames>
  179. </Storyboard>
  180. </VisualState>
  181. </VisualStateGroup>
  182. <VisualStateGroup x:Name="ButtonStates">
  183. <VisualState x:Name="ButtonVisible">
  184. <Storyboard>
  185. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="RevealButton">
  186. <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
  187. </ObjectAnimationUsingKeyFrames>
  188. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsChecked" Storyboard.TargetName="RevealButton">
  189. <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
  190. </ObjectAnimationUsingKeyFrames>
  191. </Storyboard>
  192. </VisualState>
  193. <VisualState x:Name="ButtonCollapsed">
  194. <Storyboard>
  195. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="RevealButton">
  196. <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
  197. </ObjectAnimationUsingKeyFrames>
  198. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsChecked" Storyboard.TargetName="RevealButton">
  199. <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
  200. </ObjectAnimationUsingKeyFrames>
  201. </Storyboard>
  202. </VisualState>
  203. </VisualStateGroup>
  204. </VisualStateManager.VisualStateGroups>
  205. <Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Row="1"/>
  206. <ContentPresenter x:Name="HeaderContentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Margin="{ThemeResource TextControlHeaderMarginThemeThickness}" Grid.Row="0" Style="{StaticResource HeaderContentPresenterStyle}"/>
  207. <ScrollViewer x:Name="ContentElement" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" Margin="{ThemeResource RichEditBoxTextThemeMargin}" MinHeight="{ThemeResource TextControlThemeMinHeight}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled"/>
  208. <ContentControl x:Name="PlaceholderTextContentPresenter" Content="{TemplateBinding PlaceholderText}" Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" IsTabStop="False" Margin="{ThemeResource RichEditBoxTextThemeMargin}" Padding="{TemplateBinding Padding}" Grid.Row="1"/>
  209. <!--<CheckBox x:Name="RevealButton" Foreground="{ThemeResource TextBoxForegroundHeaderThemeBrush}" HorizontalAlignment="Left" IsTabStop="False" Margin="{ThemeResource PasswordBoxCheckBoxThemeMargin}" Grid.Row="2" Visibility="Collapsed" VerticalAlignment="Top"/>-->
  210. <ToggleButton x:Name="RevealButton"
  211. Grid.Row="1"
  212. HorizontalAlignment="Right"
  213. VerticalAlignment="Top"
  214. Content="?"
  215. FontFamily="Segoe UI Symbol"
  216. Foreground="Gray"
  217. IsTabStop="False"
  218. Margin="0,-6.5,-32,0"
  219. Style="{StaticResource CustomToggleButtonStyle}"
  220. Visibility="Visible" />
  221. </Grid>
  222. </ControlTemplate>
  223. </Setter.Value>
  224. </Setter>
  225. </Style>
  226. </Page.Resources>
This is how the CustomPasswordBox looks like


Source code is included, if in case of any doubts feel free to ask.