I haven't usually posted any codes, but I do today ... against my being lazy. 今回は何となくコード載せてみます。。。
C# Code:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; namespace Custom.Silverlight.Controls
{
public class WatermarkedTextBox : TextBox
{
public WatermarkedTextBox()
: base()
{
this.NormalBackground = new SolidColorBrush(Colors.White);
this.NormalForeground = new SolidColorBrush(Colors.Black);
this.WatermarkBackground = new SolidColorBrush(Colors.White);
this.WatermarkForeground = new SolidColorBrush(Colors.Gray);
this.Watermark = "Please enter text here...";
}
private DependencyProperty WatermarkProperty =
DependencyProperty.Register("Watermark", typeof(object), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnWatermarkChanged));
private DependencyProperty WatermarkBackgroundProperty =
DependencyProperty.Register("WatermarkBackground", typeof(Brush), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnWatermarkBackgroundChanged));
private DependencyProperty WatermarkForegroundProperty =
DependencyProperty.Register("WatermarkForeground", typeof(Brush), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnWatermarkForegroundChanged));
private DependencyProperty NormalBackgroundProperty =
DependencyProperty.Register("NormalBackground", typeof(Brush), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnNormalBackgroundChanged));
private DependencyProperty NormalForegroundProperty =
DependencyProperty.Register("NormalForeground", typeof(Brush), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnNormalForegroundChanged));
private DependencyProperty ToolTipProperty =
DependencyProperty.Register("ToolTip", typeof(object), typeof(WatermarkedTextBox)
, new PropertyMetadata(OnToolTipChanged));
private static void OnWatermarkChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
private static void OnWatermarkBackgroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
private static void OnWatermarkForegroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
private static void OnNormalBackgroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
private static void OnNormalForegroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
private static void OnToolTipChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
public string Watermark
{
get { return (string)GetValue(WatermarkProperty); }
set
{
SetValue(WatermarkProperty, value);
base.Text = value;
}
}
public Brush WatermarkBackground
{
get { return (Brush)GetValue(WatermarkBackgroundProperty); }
set
{
SetValue(WatermarkBackgroundProperty, value);
base.Background = value;
}
}
public Brush WatermarkForeground
{
get { return (Brush)GetValue(WatermarkForegroundProperty); }
set
{
SetValue(WatermarkForegroundProperty, value);
base.Foreground = value;
}
}public Brush NormalBackground
{
get { return (Brush)GetValue(NormalBackgroundProperty); }
set
{
SetValue(NormalBackgroundProperty, value);
}
}
public Brush NormalForeground
{
get { return (Brush)GetValue(NormalForegroundProperty); }
set
{
SetValue(NormalForegroundProperty, value);
}
}
public string ToolTip
{
get { return (string)GetValue(ToolTipProperty); }
set
{
SetValue(ToolTipProperty, value);
ToolTipService.SetToolTip(this, value);
}
}
public new Brush Background { set; get; }
public new Brush Foreground { set; get; }
public new string Text
{
set
{
base.Text = value;
}
get
{
if (Watermark == base.Text)
return null;
return base.Text;
}
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.Background = this.NormalBackground;
base.Foreground = this.NormalForeground;
if (base.Text == Watermark)
base.Text = "";
base.OnGotFocus(e);
}
protected override void OnLostFocus(RoutedEventArgs e)
{
if (String.IsNullOrEmpty(base.Text) || base.Text == Watermark)
{
base.Text = Watermark;
base.Background = WatermarkBackground;
base.Foreground = WatermarkForeground;
}
base.OnLostFocus(e);
}
}
} |
XAML:
<controls:WatermarkedTextBox x:Name="XXXXXX"
Watermark="テキストを入力してください。"
ToolTip="テキストを入力してください。"
WatermarkBackground="#FFFFFF00"
WatermarkForeground="#FFFF0000" /> |
Well, I even tried creating another controls like the watermarkedTextBox, such as WatermarkedComboBox, MaskedTextBox, NumericMaskedTextBox, DateTimeMaskedTextBox in "Silverlight 4 Beta".
コード載せるのめんどくせぇ~~~ >_<
No comments:
Post a Comment