C# 设置WPF CustomControl的默认ControlTemplate

C# 设置WPF CustomControl的默认ControlTemplate,c#,wpf,custom-controls,controltemplate,C#,Wpf,Custom Controls,Controltemplate,我设计了一些WPF CustomControl,并为它们编写了一个ControlTemplate,我通过样式将ControlTemplates分配给这些控件: 例如: public class BootstrapText : TextBox { protected override void OnLostFocus(RoutedEventArgs e) { // ... } protected override void OnTe

我设计了一些WPF CustomControl,并为它们编写了一个ControlTemplate,我通过样式将ControlTemplates分配给这些控件:

例如:

 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)));
     }
 }
ControlTemplate(实际上是样式)类似于:

 <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>

而我的样式中没有
x:Key=“SomeKey”
。此样式将影响所有
BootstrapText
s

这很好。但我还有一个问题。在表单中的某个地方,我想设置这些控件的样式,并为它们设置一些边距和填充,但保留默认值不变。但我的控制权将消失

我认为样式设置会隐藏默认样式。 在标准控件中,如
文本框
按钮
。。。每当我们写一些风格,一切都是好的,风格只是设置我们的属性


我认为,我必须将我的ControlTemplate直接分配给CustomControl,因为我必须熟悉这个样式化过程。但我不知道如何定义?

控件的默认
样式应在名为
generic.xaml的
ResourceDictionary
中定义,并且该
ResourceDictionary
应位于默认定义控件的程序集根目录下名为
Themes
的文件夹中


这些名称是按惯例命名的,所以只需将
样式
移动到
主题/generic.xaml

试试“UserControl”。在这种情况下使用非常方便。阅读更多:@mm8的回答非常好。很高兴知道这些秘密。谢谢!!!那太好了。所以如果我有多个控件;我是否应该将整个样式放在这个文件中(
generic.xaml
我的意思是)。是否可以将它们放在单独的文件中?您可以使用MergedDictionaries属性将多个不同的资源合并到generic.xaml.MergedDictionaries中,而这些资源对我不起作用。我将此添加到generic.xaml:
中,但出现运行时错误:
无法找到资源“panel.xaml”
(panel.xaml位于Themes文件夹中;我尝试更改其位置并再次测试,但仍不起作用)我发现generic.xaml中不接受相对源。我将我的resourceDictionary替换为generic.xaml中的
,现在可以使用了。