silverlight重写TextBox PassWordBox
密码框样式引入命名空间 xmlns:sys="clr-namespace:System;assembly=mscorlib"
1 <Style x:Key="MyTextBoxStyle" TargetType="TextBox"> 2 3 <Setter Property="BorderThickness" Value="0"/> 4 5 <Setter Property="Background" Value="#FFFFFFFF"/> 6 7 <Setter Property="Foreground" Value="#FF000000"/> 8 9 <Setter Property="Padding" Value="2"/> 10 11 <Setter Property="BorderBrush"> 12 13 <Setter.Value> 14 15 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 16 17 <GradientStop Color="#FFA3AEB9" Offset="0"/> 18 19 <GradientStop Color="#FF8399A9" Offset="0.375"/> 20 21 <GradientStop Color="#FF718597" Offset="0.375"/> 22 23 <GradientStop Color="#FF617584" Offset="1"/> 24 25 </LinearGradientBrush> 26 27 </Setter.Value> 28 29 </Setter> 30 31 <Setter Property="Template"> 32 33 <Setter.Value> 34 35 <ControlTemplate TargetType="TextBox"> 36 37 <Grid x:Name="RootElement"> 38 39 <vsm:VisualStateManager.VisualStateGroups> 40 41 <vsm:VisualStateGroup x:Name="CommonStates"> 42 43 <vsm:VisualState x:Name="Normal"/> 44 45 <vsm:VisualState x:Name="MouseOver"> 46 47 <Storyboard> 48 49 <ColorAnimation Storyboard.TargetName="MouseOverBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/> 50 51 </Storyboard> 52 53 </vsm:VisualState> 54 55 <vsm:VisualState x:Name="Disabled"> 56 57 <Storyboard> 58 59 <DoubleAnimation Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 60 61 </Storyboard> 62 63 </vsm:VisualState> 64 65 <vsm:VisualState x:Name="ReadOnly"> 66 67 <Storyboard> 68 69 <DoubleAnimation Storyboard.TargetName="ReadOnlyVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0" /> 70 71 </Storyboard> 72 73 </vsm:VisualState> 74 75 </vsm:VisualStateGroup> 76 77 <vsm:VisualStateGroup x:Name="FocusStates"> 78 79 <vsm:VisualState x:Name="Focused"> 80 81 <Storyboard> 82 83 <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 84 85 </Storyboard> 86 87 </vsm:VisualState> 88 89 <vsm:VisualState x:Name="Unfocused"> 90 91 <Storyboard> 92 93 <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/> 94 95 </Storyboard> 96 97 </vsm:VisualState> 98 99 </vsm:VisualStateGroup> 100 101 102 <!--<vsm:VisualStateGroup x:Name="ValidationStates"> 103 104 <vsm:VisualState x:Name="Valid"/> 105 106 <vsm:VisualState x:Name="InvalidUnfocused"> 107 108 <Storyboard> 109 110 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 111 112 <DiscreteObjectKeyFrame KeyTime="0"> 113 114 <DiscreteObjectKeyFrame.Value> 115 116 <Visibility>Visible</Visibility> 117 118 </DiscreteObjectKeyFrame.Value> 119 120 </DiscreteObjectKeyFrame> 121 122 </ObjectAnimationUsingKeyFrames> 123 124 </Storyboard> 125 126 </vsm:VisualState> 127 128 <vsm:VisualState x:Name="InvalidFocused"> 129 130 <Storyboard> 131 132 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 133 134 <DiscreteObjectKeyFrame KeyTime="0"> 135 136 <DiscreteObjectKeyFrame.Value> 137 138 <Visibility>Visible</Visibility> 139 140 </DiscreteObjectKeyFrame.Value> 141 142 </DiscreteObjectKeyFrame> 143 144 </ObjectAnimationUsingKeyFrames> 145 146 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen"> 147 148 <DiscreteObjectKeyFrame KeyTime="0"> 149 150 <DiscreteObjectKeyFrame.Value> 151 152 <sys:Boolean>True</sys:Boolean> 153 154 </DiscreteObjectKeyFrame.Value> 155 156 </DiscreteObjectKeyFrame> 157 158 </ObjectAnimationUsingKeyFrames> 159 160 </Storyboard> 161 162 </vsm:VisualState> 163 164 </vsm:VisualStateGroup>--> 165 166 167 168 </vsm:VisualStateManager.VisualStateGroups> 169 170 171 172 173 174 <Border x:Name="Border" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Opacity="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"> 175 176 <Grid> 177 178 <Border x:Name="ReadOnlyVisualElement" Opacity="0" Background="#5EC9C9C9"/> 179 180 <Border x:Name="MouseOverBorder" BorderThickness="0" BorderBrush="Transparent"> 181 182 <ScrollViewer x:Name="ContentElement" Padding="{TemplateBinding Padding}" BorderThickness="0" IsTabStop="False"/> 183 184 </Border> 185 186 </Grid> 187 188 </Border> 189 190 <Border x:Name="DisabledVisualElement" Background="#A5F7F7F7" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" IsHitTestVisible="False"/> 191 192 <Border x:Name="FocusVisualElement" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" Margin="1" Opacity="0" IsHitTestVisible="False"/> 193 194 195 <!--<Border x:Name="ValidationErrorElement" BorderThickness="1" CornerRadius="1" BorderBrush="#FFDB000C" Visibility="Collapsed"> 196 197 <ToolTipService.ToolTip> 198 199 <ToolTip x:Name="validationTooltip" Template="{StaticResource ValidationToolTipTemplate}" Placement="Right" 200 201 PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" 202 203 DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> 204 205 <ToolTip.Triggers> 206 207 <EventTrigger RoutedEvent="Canvas.Loaded"> 208 209 <EventTrigger.Actions> 210 211 <BeginStoryboard> 212 213 <Storyboard> 214 215 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible"> 216 217 <DiscreteObjectKeyFrame KeyTime="0"> 218 219 <DiscreteObjectKeyFrame.Value> 220 221 <sys:Boolean>true</sys:Boolean> 222 223 </DiscreteObjectKeyFrame.Value> 224 225 </DiscreteObjectKeyFrame> 226 227 </ObjectAnimationUsingKeyFrames> 228 229 </Storyboard> 230 231 </BeginStoryboard> 232 233 </EventTrigger.Actions> 234 235 </EventTrigger> 236 237 </ToolTip.Triggers> 238 239 </ToolTip> 240 241 </ToolTipService.ToolTip> 242 243 <Grid Width="12" Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Background="Transparent"> 244 245 <Path Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C"/> 246 247 <Path Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff"/> 248 249 </Grid> 250 251 </Border>--> 252 253 254 255 256 </Grid> 257 258 </ControlTemplate> 259 260 </Setter.Value> 261 262 </Setter> 263 264 </Style> 265 266 <Style x:Key="MyPasswordBoxStyle" TargetType="PasswordBox"> 267 <Setter Property="BorderThickness" Value="0" /> 268 <Setter Property="Background" Value="#FFFFFFFF" /> 269 <Setter Property="Foreground" Value="#FF000000" /> 270 <Setter Property="Padding" Value="2" /> 271 <Setter Property="BorderBrush"> 272 <Setter.Value> 273 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 274 <GradientStop Color="#FFA3AEB9" Offset="0"/> 275 <GradientStop Color="#FF8399A9" Offset="0.375"/> 276 <GradientStop Color="#FF718597" Offset="0.375"/> 277 <GradientStop Color="#FF617584" Offset="1"/> 278 </LinearGradientBrush> 279 </Setter.Value> 280 </Setter> 281 <Setter Property="Template"> 282 <Setter.Value> 283 <ControlTemplate TargetType="PasswordBox"> 284 <Grid x:Name="RootElement"> 285 <vsm:VisualStateManager.VisualStateGroups> 286 <vsm:VisualStateGroup x:Name="CommonStates"> 287 <vsm:VisualState x:Name="Normal" /> 288 <vsm:VisualState x:Name="MouseOver"> 289 <Storyboard> 290 <ColorAnimation Storyboard.TargetName="MouseOverBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/> 291 </Storyboard> 292 </vsm:VisualState> 293 <vsm:VisualState x:Name="Disabled"> 294 <Storyboard> 295 <DoubleAnimation Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 296 </Storyboard> 297 </vsm:VisualState> 298 </vsm:VisualStateGroup> 299 <vsm:VisualStateGroup x:Name="FocusStates"> 300 <vsm:VisualState x:Name="Focused"> 301 <Storyboard> 302 <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 303 </Storyboard> 304 </vsm:VisualState> 305 <vsm:VisualState x:Name="Unfocused"> 306 <Storyboard> 307 <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/> 308 </Storyboard> 309 </vsm:VisualState> 310 </vsm:VisualStateGroup> 311 <vsm:VisualStateGroup x:Name="ValidationStates"> 312 <vsm:VisualState x:Name="Valid"/> 313 <vsm:VisualState x:Name="InvalidUnfocused"> 314 <Storyboard> 315 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 316 <DiscreteObjectKeyFrame KeyTime="0" > 317 <DiscreteObjectKeyFrame.Value> 318 <Visibility>Visible</Visibility> 319 </DiscreteObjectKeyFrame.Value> 320 </DiscreteObjectKeyFrame> 321 </ObjectAnimationUsingKeyFrames> 322 </Storyboard> 323 </vsm:VisualState> 324 <vsm:VisualState x:Name="InvalidFocused"> 325 <Storyboard> 326 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 327 <DiscreteObjectKeyFrame KeyTime="0" > 328 <DiscreteObjectKeyFrame.Value> 329 <Visibility>Visible</Visibility> 330 </DiscreteObjectKeyFrame.Value> 331 </DiscreteObjectKeyFrame> 332 </ObjectAnimationUsingKeyFrames> 333 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen"> 334 <DiscreteObjectKeyFrame KeyTime="0" > 335 <DiscreteObjectKeyFrame.Value> 336 <sys:Boolean>True</sys:Boolean> 337 </DiscreteObjectKeyFrame.Value> 338 </DiscreteObjectKeyFrame> 339 </ObjectAnimationUsingKeyFrames> 340 </Storyboard> 341 </vsm:VisualState> 342 </vsm:VisualStateGroup> 343 </vsm:VisualStateManager.VisualStateGroups> 344 345 <Border x:Name="Border" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Opacity="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"> 346 <Border x:Name="MouseOverBorder" BorderThickness="0" BorderBrush="Transparent"> 347 <Border x:Name="ContentElement" Margin="{TemplateBinding Padding}"/> 348 </Border> 349 </Border> 350 <Border x:Name="DisabledVisualElement" Background="#A5F7F7F7" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" IsHitTestVisible="False"/> 351 <Border x:Name="FocusVisualElement" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" Margin="1" Opacity="0" IsHitTestVisible="False"/> 352 <!--<Border x:Name="ValidationErrorElement" BorderThickness="1" CornerRadius="1" BorderBrush="#FFDB000C" Visibility="Collapsed"> 353 <ToolTipService.ToolTip> 354 <ToolTip x:Name="validationTooltip" Template="{StaticResource ValidationToolTipTemplate}" Placement="Right" 355 PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" 356 DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> 357 <ToolTip.Triggers> 358 <EventTrigger RoutedEvent="Canvas.Loaded"> 359 <EventTrigger.Actions> 360 <BeginStoryboard> 361 <Storyboard> 362 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible"> 363 <DiscreteObjectKeyFrame KeyTime="0" > 364 <DiscreteObjectKeyFrame.Value> 365 <sys:Boolean>true</sys:Boolean> 366 </DiscreteObjectKeyFrame.Value> 367 </DiscreteObjectKeyFrame> 368 </ObjectAnimationUsingKeyFrames> 369 </Storyboard> 370 </BeginStoryboard> 371 </EventTrigger.Actions> 372 </EventTrigger> 373 </ToolTip.Triggers> 374 </ToolTip> 375 </ToolTipService.ToolTip> 376 <Grid Width="12" Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Background="Transparent"> 377 <Path Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C"/> 378 <Path Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff"/> 379 </Grid> 380 </Border>--> 381 </Grid> 382 </ControlTemplate> 383 </Setter.Value> 384 </Setter> 385 </Style>