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 ?

RezaNoei
  • 1,266
  • 1
  • 8
  • 24

1 Answers1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 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 ? – RezaNoei Aug 16 '18 at 14:49
  • You can use the MergedDictionaries property to merge several different resources into generic.xaml. – mm8 Aug 16 '18 at 14:50
  • MergedDictionaries not working for me. I added this into generic.xaml : `` 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) – RezaNoei Aug 16 '18 at 22:46
  • I have found that Relative source doesn't accepted in generic.xaml. I replaced my resourceDictionary to `` in generic.xaml and its working now. – RezaNoei Aug 17 '18 at 08:44