2010-05-25 109 views
0

我想对复选框的默认模板做一些小的调整。现在我明白如何从头开始创建一个新模板,但我不知道。我设法(我想?)通过方法here提取默认模板。有没有办法根据默认模板编辑控件的模板?

它吐出来:

<ControlTemplate TargetType="CheckBox" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:s="clr-namespace:System;assembly=mscorlib" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"> 
     <BulletDecorator Background="#00FFFFFF" SnapsToDevicePixels="True"> 
      <BulletDecorator.Bullet> 
       <mwt:BulletChrome Background="{TemplateBinding Panel.Background}" 
            BorderBrush="{TemplateBinding Border.BorderBrush}" 
            BorderThickness="{TemplateBinding Border.BorderThickness}" 
            RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" 
            RenderPressed="{TemplateBinding ButtonBase.IsPressed}" 
            IsChecked="{TemplateBinding ToggleButton.IsChecked}" /> 
      </BulletDecorator.Bullet> 
      <ContentPresenter RecognizesAccessKey="True" 
           Content="{TemplateBinding ContentControl.Content}" 
           ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
           ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
           Margin="{TemplateBinding Control.Padding}" 
           HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
           VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
     </BulletDecorator> 
     <ControlTemplate.Triggers> 
      <Trigger Property="ContentControl.HasContent"> 
       <Setter Property="FrameworkElement.FocusVisualStyle"> 
        <Setter.Value> 
         <Style TargetType="IFrameworkInputElement"> 
          <Style.Resources> 
           <ResourceDictionary /> 
          </Style.Resources> 
          <Setter Property="Control.Template"> 
           <Setter.Value> 
            <ControlTemplate> 
             <Rectangle Stroke="#FF000000" 
                StrokeThickness="1" 
                StrokeDashArray="1 2" 
                Margin="14,0,0,0" 
                SnapsToDevicePixels="True" /> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="Control.Padding"> 
        <Setter.Value> 
         <Thickness>2,0,0,0</Thickness> 
        </Setter.Value> 
       </Setter> 
       <Trigger.Value> 
        <s:Boolean>True</s:Boolean> 
       </Trigger.Value> 
      </Trigger> 
      <Trigger Property="UIElement.IsEnabled"> 
       <Setter Property="TextElement.Foreground"> 
        <Setter.Value> 
         <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
        </Setter.Value> 
       </Setter> 
       <Trigger.Value> 
        <s:Boolean>False</s:Boolean> 
       </Trigger.Value> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

好吧,当然,看起来只有精细,我猜。我没有足够的经验来知道这看起来是否正确。现在,我得到两个错误:

大会 'PresentationFramework.Luna' 没有被发现。确认你不是 缺少装配参考。此外, 验证您的项目和所有 引用程序集已建成。

类型 'MWT:BulletChrome' 没有 发现。确认您没有错过 程序集参考,并且所有 引用程序集都已构建。

现在我想知道,我该如何解决这些错误,以便我可以真正开始在模板上工作?有没有更好的方法来解决这个问题?我的老板想要一个带红色方块而不是绿色的三态复选框,他不会拒绝回答。

回答

2

要解决这两个错误,添加到PresentationFramework.Luna引用(这是在GAC所以你会选择当VS添加引用在.NET选项卡中找到它)。

但是,请注意,月神主题将只默认使用如果Windows XP的主题是月神(默认的)。如果您在显示设置中更改主题,则您复选框将保留Luna主题,因为您重用了Luna模板。

时如果所述模板还没有被摆在首位设计成这样只能编辑现有模板的一部分,没有别的办法。

+0

我很好,让我的复选框走出“主题”。更重要的是我只是想改变那个盒子是红色的! – 2010-05-25 20:29:07

2

修改现有的模板的各部分的唯一方法是完全地覆盖(的复制)。你可以很容易地做到这一点(它会立即编译 - 无需手动引用任何内容)与表达式混合(编辑模板 - >编辑副本)。

有你必须保持介意与CONTROLTEMPLATES一两件事:

没有为每个称为 OS主题(月神,航空等)单独的ControlTemplate因此控制将寻找正确的每个下OS主题。如果你重写它,那么情况就不是这样了,它在每个操作系统下的每个主题下总会看起来一样。在你的情况下,即使你在Vista,Win7或Battleship-Gray-Theme上运行应用程序,它总是看起来像Windows XP Luna(因为你选择Luna ControlTemplate作为起点)。

所以在一定程度上覆盖CONTROLTEMPLATES是由设计缺陷。当然,你可以为每个已知的操作系统主题提供一个ControlTemplate,但是如果有新的操作系统,这将无济于事。

0

Windows SDK中包含的所有控件模板都不是完整的副本吗?