waterMarkTextBox

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MyWPFCustomControls"
                   
                    >
  
    
    <Style  TargetType="{x:Type local:WaterMarkTextBox}">
        <Setter Property="Background" Value="#ffffff"/>
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="BorderBrush" Value="#5d7fad"></Setter>
        <Setter Property="FontWeight" Value="Normal"></Setter>
        <Setter Property="Foreground" Value="#0c223a"></Setter>
        <Setter Property="Padding" Value="5,0,5,2"></Setter>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="FontSize" Value="14"></Setter>
        <Setter Property="FontFamily" Value="Arial"></Setter>
        <Setter Property="FlowDirection" Value="LeftToRight"></Setter>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:WaterMarkTextBox}" >
                    <Border Background="{TemplateBinding Background}" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                        <Grid>
                          
                            <ScrollViewer   x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <ContentPresenter x:Name="PART_WatermarkHost"  Content="{TemplateBinding Watermark}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                <ContentPresenter.ContentTemplate>
                                    <DataTemplate>
                                        <ContentControl Content="{Binding}" Foreground="Gray" Focusable="False"/>
                                    </DataTemplate>
                                </ContentPresenter.ContentTemplate>
                            </ContentPresenter>
                        </Grid>

                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsFocused" Value="False"/>
                                <Condition Property="Text" Value=""/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" TargetName="PART_WatermarkHost" Value="Visible"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True" >
                            <Setter TargetName="Bd" Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#fe1C67C7"></DropShadowEffect>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Bd"  Property="Effect">
                                <Setter.Value>
                                    <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#Fe9DBADF"></DropShadowEffect>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="Height" Value="Auto">
                            <Setter Property="MinHeight" Value="28"/>
                        </Trigger>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter TargetName="Bd" Property="BorderBrush" Value="Red"/>
                            <Setter Property="ToolTip">
                                <Setter.Value>
                                    <Binding 
                            Path="(Validation.Errors).CurrentItem.ErrorContent"
                            RelativeSource="{x:Static RelativeSource.Self}"
                            />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="1">
            <!--<Setter.Value>
                <Thickness Bottom="2" Left="1" Right="1" Top="1"></Thickness>
            </Setter.Value>-->
        </Setter>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>

            <!--<Trigger Property="Attach:AttachProperty.IsMandatory" Value="True">
                <Setter Property="Background" Value="LightYellow"/>
            </Trigger>-->
            <!--<Trigger Property="IsFocused" Value="True" >
                <Setter Property="Button.Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#fe1C67C7"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Button.Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#Fe9DBADF"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>-->
            <!--<Trigger Property="Validation.HasError" Value="True">
               
                <Setter Property="FontFamily" Value="Arial"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="ForceCursor" Value="False"/>
                <Setter Property="Foreground" Value="Gray"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Padding" Value="5,0,5,2"></Setter>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="MinHeight" Value="28"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">                           
                                <Border Background="#ffffff" BorderBrush="Red" x:Name="Bd" CornerRadius="5" BorderThickness="1">
                                    <Grid>
                                        <TextBlock x:Name="PART_MyWaterMarkTextBlock" Visibility="Collapsed" VerticalAlignment="Center" HorizontalAlignment="Left" Padding="5,0,0,3" Foreground="Gray"/>
                                        <ScrollViewer  x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                    </Grid>
                                </Border>
                                

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>-->
        </Style.Triggers>
    </Style>

</ResourceDictionary>
View Code

xaml

 public  class WaterMarkTextBox:TextBox
    {



        public string Watermark
        {
            get { return (string)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Watermark.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.Register("Watermark", typeof(string), typeof(WaterMarkTextBox), new PropertyMetadata(null));

        

      public WaterMarkTextBox()
      {

      }
    }
View Code

cs

原文地址:https://www.cnblogs.com/FaDeKongJian/p/3247387.html