【WPF系列】Textbox_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 【WPF系列】Textbox

【WPF系列】Textbox

 2015/3/4 17:01:05  Look Into Coding  程序员俱乐部  我要评论(0)
  • 摘要:Style定义实例给Textbox定义一个阴影效果。<Stylex:Key="{x:TypeTextBox}"TargetType="{x:TypeTextBox}"><SetterProperty="SnapsToDevicePixels"Value="True"/><SetterProperty="OverridesDefaultStyle"
  • 标签:

Style定义实例

给Textbox定义一个阴影效果。

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
         <Setter Property="SnapsToDevicePixels" Value="True"/>
         <Setter Property="OverridesDefaultStyle" Value="True"/>
         <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
         <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
         <Setter Property="MinWidth" Value="120"/>
         <Setter Property="MinHeight" Value="20"/>
         <Setter Property="AllowDrop" Value="true"/>
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type TextBoxBase}">
               <Border x:Name="Border"                                              
                     BorderThickness="1"
                     CornerRadius="2"
                     Padding="0">
                     <Border.BorderBrush>
                     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
  <GradientStop Color="#888888" Offset="0" />
  <GradientStop Color="#AAAAAA" Offset=".2" />
</LinearGradientBrush>
                     </Border.BorderBrush>
                     <ScrollViewer x:Name="PART_ContentHost" Margin="0">
                        <ScrollViewer.Background>
                           <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                              <GradientStop Offset="0" Color="WhiteSmoke"/>
                              <GradientStop Offset="1" Color="LightGray"/>
                           </LinearGradientBrush>
                        </ScrollViewer.Background>
                     </ScrollViewer>
                  </Border>
                 
                  <ControlTemplate.Triggers>
                     <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
                        <Setter Property="Foreground" Value="#888888"/>
                     </Trigger>
                  </ControlTemplate.Triggers>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>

 

UpdateSourceTrigger

 

默认UpdateSourceTrigger为LostFoucs,有时需要及时CommitValue,则需要设置为PropertyChanged。这样当Text属性的值发生变化时,我们的值就能及时更新到Datasource中。

 

NumberTextbox

  1. 使用NubmberTextboxBehavior
  2. 将TextBox的binding属性中Delay设置为1000

 

 

WaterMark/HintText/PlaceHoder

  1. 通过Style
  2. 通过AttachBehavior
  3. 通过自定义控件

1.给TextBox添加水印效果(提示文字)

<!--Add a placeHolder for TextBox using Tag value-->
    <Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <TextBox Text="{Binding Path=Text,
                                                RelativeSource={RelativeSource TemplatedParent},
                                                Mode=TwoWay,
                                                UpdateSourceTrigger=PropertyChanged,
                                                Delay=1000}"
                                 x:Name="textSource"
                                 Background="Transparent"
                                 Panel.ZIndex="2" />
                        <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <Setter Property="Foreground" Value="Transparent" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                            <Setter Property="Foreground" Value="LightGray" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

 

 

 

参考

WPF validation rule preventing decimal entry in textbox?

Set WPF Binding.StringFormat Property on TextBox via Style

  • 相关文章
发表评论
用户名: 匿名