Blend 4中经常看到一个输入框,里面有值还有单位,当获取到焦点的时候,就只有值了,修改完值之后,失去焦点,单位又加上了。
class myTextbox : TextBox { private int _timeValue=100; public int TimeValue { get { return _timeValue; } set { _timeValue = value; } } public myTextbox() { this.Text = TimeValue.ToString()+" 秒"; GotFocus += myTextbox_GotFocus; LostFocus += myTextbox_LostFocus; } void myTextbox_LostFocus(object sender, System.Windows.RoutedEventArgs e) { TimeValue = Convert.ToInt32(this.Text.ToString()); this.Text = TimeValue.ToString() + " 秒"; } void myTextbox_GotFocus(object sender, System.Windows.RoutedEventArgs e) { this.Text = TimeValue.ToString(); } }
这样就实现了...