自定义控件,重写 TextBox 实例

项目中可能会遇到重写控件的情况,特此记录下:

 1 "WpfApp6.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp6"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="157" Width="287">
 9     
10         
28     
29     
30         
31             "852"/>
32         
33     
34 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace WpfApp6
17 {
18     /// 
19     /// MainWindow.xaml 的交互逻辑
20     /// 
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27     }
28 
29 
30     public class CustomTextBox : TextBox
31     {
32         public override void OnApplyTemplate()
33         {
34             base.OnApplyTemplate();
35 
36             var btnclear = GetTemplateChild("btnclear") as Button;
37             btnclear.Click += Btnclear_Click;
38         }
39 
40         private void Btnclear_Click(object sender, RoutedEventArgs e)
41         {
42             Text = string.Empty;
43         }
44     }
45 }

 

你可能感兴趣的:(自定义控件,重写 TextBox 实例)