You could put the code in CustomControl, here is an example. I replaced IsEnable with IsMouseOver to show the demo effect.
Add CustomControl:
Right click on the project and add CustomControl named MyTextBox.
(You can also use WPF custom control library, please pay attention to the namespace reference when using.)
Project structure:
Generic.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControlDemo">
<ControlTemplate x:Key="TextBoxEnabledTemplate" TargetType="{x:Type local:MyTextBox}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="120"/>
<ColumnDefinition Width="20" MinWidth="0"/>
</Grid.ColumnDefinitions>
<ToggleButton Grid.Column="0" Background="AliceBlue" Grid.ColumnSpan="2"/>
<TextBox Grid.Column="0" Background="Wheat"/>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type local:MyTextBox}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyTextBox}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="120"/>
</Grid.ColumnDefinitions>
<ToggleButton Grid.Column="0" Background="AliceBlue"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Template" Value="{StaticResource TextBoxEnabledTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
MyTextBox.cs:
public class MyTextBox : TextBox
{
static MyTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(typeof(MyTextBox)));
}
}
MainWindow.xaml:
<StackPanel>
<local:MyTextBox x:Name="tb1" Width="200" Height="100" />
<local:MyTextBox x:Name="tb2" Width="200" Height="100" />
</StackPanel>
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html