4 changed files with 144 additions and 0 deletions
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using System.Windows; |
||||
|
using System.Windows.Controls; |
||||
|
|
||||
|
namespace Microsoft.Windows.Controls |
||||
|
{ |
||||
|
public class WatermarkTextBox : TextBox |
||||
|
{ |
||||
|
#region Properties
|
||||
|
|
||||
|
#region Watermark
|
||||
|
|
||||
|
public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register("Watermark", typeof(object), typeof(WatermarkTextBox), new UIPropertyMetadata(null)); |
||||
|
public object Watermark |
||||
|
{ |
||||
|
get { return (object)GetValue(WatermarkProperty); } |
||||
|
set { SetValue(WatermarkProperty, value); } |
||||
|
} |
||||
|
|
||||
|
#endregion //Watermark
|
||||
|
|
||||
|
#region WatermarkTemplate
|
||||
|
|
||||
|
public static readonly DependencyProperty WatermarkTemplateProperty = DependencyProperty.Register("WatermarkTemplate", typeof(DataTemplate), typeof(WatermarkTextBox), new UIPropertyMetadata(null)); |
||||
|
public DataTemplate WatermarkTemplate |
||||
|
{ |
||||
|
get { return (DataTemplate)GetValue(WatermarkTemplateProperty); } |
||||
|
set { SetValue(WatermarkTemplateProperty, value); } |
||||
|
} |
||||
|
|
||||
|
#endregion //WatermarkTemplate
|
||||
|
|
||||
|
#endregion //Properties
|
||||
|
|
||||
|
#region Constructors
|
||||
|
|
||||
|
static WatermarkTextBox() |
||||
|
{ |
||||
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(WatermarkTextBox), new FrameworkPropertyMetadata(typeof(WatermarkTextBox))); |
||||
|
} |
||||
|
|
||||
|
#endregion //Constructors
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,93 @@ |
|||||
|
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
|
xmlns:local="clr-namespace:Microsoft.Windows.Controls"> |
||||
|
|
||||
|
<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"> |
||||
|
<GradientStop Color="#ABADB3" Offset="0.05"/> |
||||
|
<GradientStop Color="#E2E3EA" Offset="0.07"/> |
||||
|
<GradientStop Color="#E3E9EF" Offset="1"/> |
||||
|
</LinearGradientBrush> |
||||
|
|
||||
|
<LinearGradientBrush x:Key="TextBox_MouseOver" EndPoint="0,1" StartPoint="0,0"> |
||||
|
<GradientStop Color="#3C7FB1" Offset="0.05"/> |
||||
|
<GradientStop Color="#98D6FA" Offset="0.07"/> |
||||
|
<GradientStop Color="#AEDDF8" Offset="1"/> |
||||
|
</LinearGradientBrush> |
||||
|
|
||||
|
<LinearGradientBrush x:Key="TextBox_Focused" EndPoint="0,1" StartPoint="0,0"> |
||||
|
<GradientStop Color="#3C7FB1" Offset="0.05"/> |
||||
|
<GradientStop Color="#98D6FA" Offset="0.07"/> |
||||
|
<GradientStop Color="#AEDDF8" Offset="1"/> |
||||
|
</LinearGradientBrush> |
||||
|
|
||||
|
<DataTemplate x:Key="DefaultWatermarkTemplate"> |
||||
|
<ContentControl Content="{Binding}" Foreground="Gray" /> |
||||
|
</DataTemplate> |
||||
|
|
||||
|
<Style TargetType="{x:Type local:WatermarkTextBox}"> |
||||
|
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> |
||||
|
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> |
||||
|
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> |
||||
|
<Setter Property="BorderThickness" Value="1"/> |
||||
|
<Setter Property="Padding" Value="3" /> |
||||
|
<Setter Property="AllowDrop" Value="true"/> |
||||
|
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> |
||||
|
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> |
||||
|
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/> |
||||
|
<Setter Property="WatermarkTemplate" Value="{StaticResource DefaultWatermarkTemplate}" /> |
||||
|
<Setter Property="Template"> |
||||
|
<Setter.Value> |
||||
|
<ControlTemplate TargetType="{x:Type local:WatermarkTextBox}"> |
||||
|
<Grid> |
||||
|
|
||||
|
<Border x:Name="Border" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="1.5" Background="{TemplateBinding Background}"> |
||||
|
|
||||
|
</Border> |
||||
|
|
||||
|
<Border x:Name="MouseOverVisual" Opacity="0" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{StaticResource TextBox_MouseOver}" CornerRadius="1.5" > |
||||
|
|
||||
|
</Border> |
||||
|
|
||||
|
<Border x:Name="FocusVisual" Opacity="0" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{StaticResource TextBox_Focused}" CornerRadius="1.5" > |
||||
|
|
||||
|
</Border> |
||||
|
|
||||
|
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> |
||||
|
|
||||
|
<ContentPresenter x:Name="PART_WatermarkHost" |
||||
|
Content="{TemplateBinding Watermark}" |
||||
|
ContentTemplate="{TemplateBinding WatermarkTemplate}" |
||||
|
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" |
||||
|
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" |
||||
|
IsHitTestVisible="False" |
||||
|
Margin="{TemplateBinding Padding}" |
||||
|
Visibility="Collapsed"/> |
||||
|
|
||||
|
</Grid> |
||||
|
<ControlTemplate.Triggers> |
||||
|
<MultiTrigger> |
||||
|
<MultiTrigger.Conditions> |
||||
|
<Condition Property="IsFocused" Value="False"></Condition> |
||||
|
<Condition Property="Text" Value=""></Condition> |
||||
|
</MultiTrigger.Conditions> |
||||
|
<MultiTrigger.Setters> |
||||
|
<Setter Property="Visibility" TargetName="PART_WatermarkHost" Value="Visible"></Setter> |
||||
|
</MultiTrigger.Setters> |
||||
|
</MultiTrigger> |
||||
|
<Trigger Property="IsMouseOver" Value="True"> |
||||
|
<Setter TargetName="MouseOverVisual" Property="Opacity" Value="1" /> |
||||
|
</Trigger> |
||||
|
<Trigger Property="IsFocused" Value="True"> |
||||
|
<Setter TargetName="FocusVisual" Property="Opacity" Value="1" /> |
||||
|
</Trigger> |
||||
|
<Trigger Property="IsEnabled" Value="false"> |
||||
|
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> |
||||
|
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> |
||||
|
</Trigger> |
||||
|
</ControlTemplate.Triggers> |
||||
|
</ControlTemplate> |
||||
|
</Setter.Value> |
||||
|
</Setter> |
||||
|
</Style> |
||||
|
|
||||
|
</ResourceDictionary> |
||||
Loading…
Reference in new issue