<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="1">
<Grid>
<TextBox x:Name="txtHour" Grid.Column="6" Text="00" Background="{TemplateBinding OpacityMask}" PreviewTextInput ="TextBox_PreviewTextInput"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I can't handle event PreviewTextInput in code behind. Error 'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the PreviewTextInput event, or add a x:Class attribute to the root element.
public class CustomControl: Control
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
MessageBox.Show("TEST");
}
}
This is my code behind.