I have designed some WPF CustomControl and I wrote a ControlTemplate for them, and i assigned ControlTemplates to those controls through styles :
for Example:
public class BootstrapText : TextBox { protected override void OnLostFocus(RoutedEventArgs e) { // ... } protected override void OnTextChanged(TextChangedEventArgs e) { // ... base.OnTextChanged(e); } public static readonly DependencyProperty RegexProperty = DependencyProperty.Register("Regex", typeof(string), typeof(BootstrapText), new PropertyMetadata(null)); public bool IsValid { // ... } public string Regex { // ... } static BootstrapText() { DefaultStyleKeyProperty.OverrideMetadata(typeof(BootstrapText), new FrameworkPropertyMetadata(typeof(BootstrapText))); } } and ControlTemplate (Actually Style) is Something like:
<Style TargetType="Controls:BootstrapText" BasedOn="{StaticResource FontBase}"> <Setter Property="Padding" Value="2"/> <Setter Property="Width" Value="240"/> <Setter Property="SnapsToDevicePixels" Value="True"></Setter> <Setter Property="Background" Value="#FEFEFE"/> <!--<Setter Property="VerticalContentAlignment" Value="Center"/>--> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Controls:BootstrapText"> <Grid> <Border Name="container" HorizontalAlignment="Left" Padding="{TemplateBinding Padding}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="#ccc" Background="{TemplateBinding Background}"> <ScrollViewer Padding="0" x:Name="PART_ContentHost" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.TextAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> While i have no x:Key="SomeKey" in the style. this style will affects all BootstrapTexts.
This is Good. But I have another problem. Somewhere in a Form, I want to Style these Controls and Set some Margin and Padding for them but leave the defaults as they are. but my Control will be disappeared !!!
I think twice styling will hides the default style. In the standard Controls like TextBox, Button, ... whenever we write some style, everything is good and style simply sets our properties.
I think, I must assign my ControlTemplate directly to the CustomControl Because i must get ride of this Styling process. but I don't know how ?