【问题标题】:Setting default ControlTemplate for WPF CustomControl为 WPF CustomControl 设置默认 ControlTemplate
【发布时间】:2019-01-22 19:23:01
【问题描述】:

我设计了一些 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" 的风格。此样式将影响所有BootstrapTexts。

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

我认为两次样式会隐藏默认样式。 在TextBoxButton、...等标准控件中,每当我们编写一些样式时,一切都很好,样式只是设置了我们的属性。

我认为,我必须将我的 ControlTemplate 直接分配给 CustomControl,因为我必须完成这个造型过程。但我不知道怎么做?

【问题讨论】:

  • 尝试“用户控制”。在这种情况下使用非常方便。阅读更多:Custom vs User control
  • @mm8 答案很棒。很高兴知道这些秘密。

标签: c# wpf custom-controls controltemplate


【解决方案1】:

控件的默认Style 应在名为generic.xamlResourceDictionary 中定义,此ResourceDictionary 应位于定义控件的程序集根目录下名为Themes 的文件夹中默认。

这些名称是约定俗成的,因此只需将您的 Style 移至 Themes/generic.xaml

【讨论】:

  • 谢谢!!!那很棒。所以如果我有多个控件;我应该将整个样式放在这个文件中吗(generic.xaml 我的意思是)。是否可以将它们放在单独的文件中?
  • 您可以使用 MergedDictionaries 属性将多个不同的资源合并到 generic.xaml 中。
  • MergedDictionaries 不适合我。我将此添加到 generic.xaml 中:&lt;ResourceDictionary.MergedDictionaries&gt;&lt;ResourceDictionary Source="Panel.xaml"/&gt;&lt;/ResourceDictionary.MergedDictionaries&gt; 但我有运行时错误:Cannot locate resource 'panel.xaml'(Panel.xaml 位于 Themes 文件夹中;我尝试更改它的位置并再次对其进行测试,但它仍然无法正常工作)
  • 我发现 generic.xaml 中不接受相对源。我在 generic.xaml 中将我的 resourceDictionary 替换为 &lt;ResourceDictionary Source="pack://application:,,,/Bootstrap;component/Themes/Panel.xaml"/&gt;,它现在可以工作了。
猜你喜欢
  • 2013-05-23
  • 2015-02-28
  • 2014-01-03
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
相关资源
最近更新 更多