0

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 ?

2
  • Try "UserControl". It's very convenient to use in this case. Read more: Custom vs User control Commented Aug 16, 2018 at 2:25
  • @mm8 answer was great. this is good to know the secrets. Commented Aug 16, 2018 at 14:52

1 Answer 1

1

A default Style for a control should be define in a ResourceDictionary called generic.xaml and this ResourceDictionary should be located in a folder called Themes at the root of the assembly in which the control is defined by default.

These names are by convention so just move your Style to Themes/generic.xaml.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks !!! That was great. so if i had multiple controls; should i place whole styles in this file (generic.xaml i mean). does it possible to put them in separate files ?
You can use the MergedDictionaries property to merge several different resources into generic.xaml.
MergedDictionaries not working for me. I added this into generic.xaml : <ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Panel.xaml"/></ResourceDictionary.MergedDictionaries> but i have runtime error: Cannot locate resource 'panel.xaml' (Panel.xaml is in Themes folder; i tried to change it's location and test it again but it still not working)
I have found that Relative source doesn't accepted in generic.xaml. I replaced my resourceDictionary to <ResourceDictionary Source="pack://application:,,,/Bootstrap;component/Themes/Panel.xaml"/> in generic.xaml and its working now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.