El proyecto puede experimentar el control de reescritura, bajo la inscripción decide:
1 <ventana x: Class = " 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: = locales " clr-espacio de nombres: WpfApp6 " 7 MC: se ignoran = "d " 8 Título = " MainWindow " Altura = " 157 " width = " 287 " > 9 <Window.Resources> 10 <Estilo TargetType = " locales: CustomTextBox " > 11 <Setter propiedad = " plantilla " > 12 <Setter.Value> 13 <ControlTemplate> 14 <Cuadrícula> 15 <Grid.ColumnDefinitions> dieciséis <ColumnDefinition /> 17 <ColumnDefinition width = " auto " /> 18 </Grid.ColumnDefinitions> 19 <TextBox VerticalContentAlignment = " centro " 20 Relleno = " 5 " 21 Text = " {Binding RelativeSource = {} RelativeSource TemplatedParent, Path = texto} " /> 22 <botón X:Name = " btnclear" Grid.Column = " 1 " width = " 30 " Margin = " 3 " = contenido " 清空" /> 23 </ Cuadrícula> 24 </ ControlTemplate> 25 </Setter.Value> 26 </ Setter> 27 </ estilo> 28 </Window.Resources> 29 <grid> 30 <StackPanel> 31 <local: CustomTextBox Text = "852 " /> 32 </ StackPanel> 33 </ Cuadrícula> 34 </ Ventana>
usando System; usando System.Collections.Generic; usando System.Linq; usando System.Text; usando System.Threading.Tasks; usando System.Windows; usando System.Windows.Controls; usando System.Windows.Data; usando System.Windows.Documents; usando System.Windows.Input; usando System.Windows.Media; usando System.Windows.Media.Imaging; usando System.Windows.Navigation; usando System.Windows.Shapes; espacio de nombres WpfApp6 { /// <summary> ///MainWindow.xaml的交互逻辑 /// </ summary> pública parcial de clase MainWindow: Ventana { público MainWindow () { InitializeComponent (); } } Pública clase CustomTextBox: Cuadro de texto { público anulación vacío OnApplyTemplate () { base de .OnApplyTemplate (); var btnclear = GetTemplateChild ( " btnclear " ) como botón; btnclear.Click + = Btnclear_Click; } Privada vacío Btnclear_Click ( objeto emisor, RoutedEventArgs e) { texto = cadena .Empty; } } }