自定义样式例子

代码

  1. <Application.Resources>
  2. <Style x:Key="styledTextBox" TargetType="{x:Type TextBox}">
  3. <Setter Property="HorizontalAlignment" Value="Left" />
  4. <Setter Property="Height" Value="28" />
  5. <Setter Property="Foreground" Value="White" />
  6. <Setter Property="CaretBrush" Value="White" />
  7. <Setter Property="Template">
  8. <Setter.Value>
  9. <ControlTemplate TargetType="{x:Type TextBox}">
  10. <Border
  11. Background="#0A5D7B"
  12. BorderBrush="#eee"
  13. BorderThickness="0"
  14. CornerRadius="2">
  15. <Grid>
  16. <Border
  17. x:Name="BorderBase"
  18. Background="Transparent"
  19. BorderBrush="Transparent"
  20. BorderThickness="1.4,1.4,1,1" />
  21. <Label
  22. x:Name="TextPrompt"
  23. VerticalAlignment="Center"
  24. Content="{TemplateBinding Tag}"
  25. Focusable="False"
  26. Foreground="Gray"
  27. Visibility="Collapsed" />
  28. <ScrollViewer
  29. x:Name="PART_ContentHost"
  30. Margin="2,0,0,0"
  31. VerticalAlignment="Center" />
  32. </Grid>
  33. </Border>
  34. <ControlTemplate.Triggers>
  35. <MultiTrigger>
  36. <MultiTrigger.Conditions>
  37. <Condition Property="IsFocused" Value="False" />
  38. <Condition Property="Text" Value="" />
  39. </MultiTrigger.Conditions>
  40. <Setter TargetName="TextPrompt" Property="Visibility" Value="Visible" />
  41. </MultiTrigger>
  42. <Trigger Property="IsFocused" Value="True">
  43. <Setter TargetName="BorderBase" Property="BorderThickness" Value="2.4,1.4,1,1" />
  44. </Trigger>
  45. <Trigger Property="IsEnabled" Value="False">
  46. <Setter Property="Foreground" Value="Gray" />
  47. </Trigger>
  48. </ControlTemplate.Triggers>
  49. </ControlTemplate>
  50. </Setter.Value>
  51. </Setter>
  52. </Style>
  53. </Application.Resources>
  1. <TextBox
  2. Width="222"
  3. Height="23"
  4. Style="{StaticResource styledTextBox}"
  5. Text="New TextBox"
  6. TextWrapping="Wrap" />

效果

image.png

提示字符串效果

代码

  1. <TextBox
  2. Width="200"
  3. Height="30"
  4. HorizontalAlignment="Center"
  5. VerticalAlignment="Center"
  6. VerticalContentAlignment="Center">
  7. <TextBox.Style>
  8. <Style xmlns:sys="clr-namespace:System;assembly=mscorlib" TargetType="TextBox">
  9. <Style.Resources>
  10. <VisualBrush
  11. x:Key="CueBannerBrush"
  12. AlignmentX="Left"
  13. AlignmentY="Center"
  14. Stretch="None">
  15. <VisualBrush.Visual>
  16. <Label Content="你的提示字符串" Foreground="LightGray" />
  17. </VisualBrush.Visual>
  18. </VisualBrush>
  19. </Style.Resources>
  20. <Style.Triggers>
  21. <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
  22. <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
  23. </Trigger>
  24. <Trigger Property="Text" Value="{x:Null}">
  25. <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
  26. </Trigger>
  27. <Trigger Property="IsKeyboardFocused" Value="True">
  28. <Setter Property="Background" Value="White" />
  29. </Trigger>
  30. </Style.Triggers>
  31. </Style>
  32. </TextBox.Style>
  33. </TextBox>

效果

image.png

另一种方式

如果你的工程里引用了MaterialDesignUI库的话,下面的方式更简单:

  1. <TextBox
  2. Name="txtStock"
  3. Grid.Row="0"
  4. Grid.Column="2"
  5. materialDesign:HintAssist.Hint="你的提示字符串"
  6. MaxLength="16"
  7. Text="{Binding StockCode}" />

自动高亮选中功能

代码

  1. <Grid>
  2. <TextBox
  3. Name="MyBox"
  4. Width="200"
  5. Height="23"
  6. GotFocus="UIElement_OnGotFocus"
  7. Text="sample text" />
  8. </Grid>
  1. private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
  2. {
  3. var textBox = (TextBox)sender;
  4. textBox.Dispatcher.BeginInvoke(new Action(() => textBox.SelectAll()));
  5. }

效果

textbox auto select sample.gif