Input.cs
unknown
csharp
2 years ago
2.7 kB
4
Indexable
[TemplatePart(Name = "Part_Clear", Type = typeof(Border))] [TemplatePart(Name = "Part_TxtBox", Type = typeof(TextBox))] [TemplatePart(Name = "Part_Placeholder", Type = typeof(TextBlock))] public class Input : TextBox { private Border _clearIcon; private TextBox _txtBox; private TextBlock _txtBlockPlaceholder; static Input() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Input), new FrameworkPropertyMetadata(typeof(Input))); } public Geometry Icon { get { return (Geometry)GetValue(IconProperty); } set { SetValue(IconProperty, value); } } public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(Geometry), typeof(Input), new PropertyMetadata(null)); public string Placeholder { get { return (string)GetValue(PlaceholderProperty); } set { SetValue(PlaceholderProperty, value); } } public static readonly DependencyProperty PlaceholderProperty = DependencyProperty.Register("Placeholder", typeof(string), typeof(Input), new PropertyMetadata("PLACEHOLDER")); public string TextContent { get { return (string)GetValue(TextContentProperty); } set { SetValue(TextContentProperty, value); } } public static readonly DependencyProperty TextContentProperty = DependencyProperty.Register("TextContent", typeof(string), typeof(Input), new PropertyMetadata(default(string))); public override void OnApplyTemplate() { base.OnApplyTemplate(); if (this.Template != null) { _clearIcon = this.Template.FindName("Part_Clear", this) as Border; _txtBox = this.Template.FindName("Part_TxtBox", this) as TextBox; _txtBlockPlaceholder = this.Template.FindName("Part_Placeholder", this) as TextBlock; if (_clearIcon != null) { _clearIcon.MouseLeftButtonDown += ClearIcon_MouseLeftButtonDown; } if (_txtBox != null) { _txtBox.TextChanged += TextBox_TextChanged; } } } private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { if (string.IsNullOrEmpty(_txtBox.Text)) { _txtBlockPlaceholder.Visibility = Visibility.Visible; } else { _txtBlockPlaceholder.Visibility = Visibility.Hidden; } } private void ClearIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _txtBox.Clear(); _txtBox.Focus(); } }
Editor is loading...