You could try to check if your namespace and class names are the same.If it is the same, an error will be reported. You can refer to the code below.
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:NumberEnumerateTextBox">
<Style TargetType="{x:Type local:NumberEnumerateTextBox1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:NumberEnumerateTextBox1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ScrollViewer x:Name="PART_ContentHost" />
<RepeatButton x:Name="UpCountRepeatButton" Margin="0,2,0,0" Width="9" Height="11"/>
<RepeatButton x:Name="DownCountRepeatButton" Margin="0,0,0,2" Width="Auto" Height="Auto" />
<Rectangle Opacity="0.5" x:Name="OverlayRectangle" Fill="#FF0F477B"/>
<TextBlock Opacity="0.9" Foreground="White" x:Name="OverlayTextBlock" FontSize="26" Margin="2,-3,0,0" />
</StackPanel >
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
NumberEnumerateTextBox1:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;
namespace NumberEnumerateTextBox
{
public class NumberEnumerateTextBox1 : Control
{
static NumberEnumerateTextBox1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberEnumerateTextBox1), new FrameworkPropertyMetadata(typeof(NumberEnumerateTextBox1)));
}
public RepeatButton UpCountRepeatButton = null;
public RepeatButton DownCountRepeatButton = null;
public Rectangle OverlayRectangle = null;
public TextBlock OverlayTextBlock = null;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpCountRepeatButton = GetTemplateChild("UpCountRepeatButton") as RepeatButton;
DownCountRepeatButton = GetTemplateChild("DownCountRepeatButton") as RepeatButton;
OverlayRectangle = GetTemplateChild("OverlayRectangle") as Rectangle;
OverlayTextBlock = GetTemplateChild("OverlayTextBlock") as TextBlock;
}
}
}
Add NumberEnumerateTextBox
dll to OnApplyTemplateCustomControlDemo
project.
MainWindow.xaml:
xmlns:num="clr-namespace:NumberEnumerateTextBox;assembly=NumberEnumerateTextBox"
<Grid>
<num:NumberEnumerateTextBox1 x:Name="box" Width="150" Height="150"/>
</Grid>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
box.Background = Brushes.Yellow;
}
}
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