2009-08-26 35 views
0

视觉特性,以便在我的应用程序触发动画应用到所有ToolTip S,我使用的是ControlTemplate。然而,使用ControlTemplateToolTip时失去其所有的默认视觉特性的定义,我相信,通过主题。我如何保留除我重写的所有属性?保持默认与控件模板

使用下面的代码

<ToolTip Opacity="0.8"> 
    <ToolTip.Content> 
    <StackPanel> 
     <Label FontWeight="Bold" Background="DarkSlateBlue" Foreground="White"> 
     WpfApplication1 
     </Label> 
     <Label> 
     Click to create another button 
     </Label> 
    </StackPanel> 
    </ToolTip.Content> 
</ToolTip> 

我得到的结果,我想:

alt text http://img29.imageshack.us/img29/1488/controltemplateno.png

但是,当我调整代码使用ControlTemplate像这样:

<ControlTemplate x:Key="controltemplateToolTip" TargetType="{x:Type ToolTip}"> 
    <ContentPresenter Content="{TemplateBinding Content}" /> 
    <ControlTemplate.Triggers> 
    <EventTrigger RoutedEvent="ToolTip.Loaded"> 
     <BeginStoryboard> 
     <Storyboard TargetProperty="Opacity"> 
      <DoubleAnimation From="0" To="0.8" Duration="0:0:0.2" /> 
     </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 
<Style TargetType="{x:Type ToolTip}"> 
    <Setter Property="Template" Value="{StaticResource controltemplateToolTip}" /> 
</Style> 
... 
<ToolTip> 
    <ToolTip.Content> 
    <StackPanel> 
     <Label FontWeight="Bold" Background="DarkSlateBlue" Foreground="White"> 
     WpfApplication1 
     </Label> 
     <Label> 
     Click to create another button 
     </Label> 
    </StackPanel> 
    </ToolTip.Content> 
</ToolTip> 

我收到以下内容:

alt text http://img43.imageshack.us/img43/8217/controltemplateyes.png

正如你所看到的,主题默认边框,背景等不保留。我不想明确地设置这些,因为我希望它们根据用户的主题进行调整。我该如何补救?我是否以这种错误的方式去做?

+0

另外请注意,在'ToolTip'风格中添加'BasedOn =“{StaticResource {x:Type ToolTip}}'没有帮助 – Gregyski 2009-08-27 20:07:54

回答

2

不能从主题风格继承控件模板。但你不必这样做。使用DataTemplates来实现你想要的。下面是一个简单的例子:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Page.Resources> 
    <Style TargetType="{x:Type ToolTip}"> 
    <Style.Triggers> 
    <EventTrigger RoutedEvent="ToolTip.Loaded"> 
     <BeginStoryboard> 
     <Storyboard TargetProperty="Opacity"> 
      <DoubleAnimation From="0" To="0.8" Duration="0:0:0.5" /> 
     </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
    </Style.Triggers> 
    </Style> 

<DataTemplate x:Key="ToolTipContentTemplate"> 
<StackPanel> 
     <Label FontWeight="Bold" Background="DarkSlateBlue" Foreground="White"> 
     WpfApplication1 
     </Label> 
     <Label> 
     Click to create another button 
     </Label> 
    </StackPanel> 
</DataTemplate> 
    </Page.Resources> 

    <Grid> 
<Button Content="Hello"> 
<Button.ToolTip> 
<ToolTip ContentTemplate="{StaticResource ToolTipContentTemplate}"> 
    <ToolTip.Content> 
    Doesn't matter in this case 
    </ToolTip.Content> 
</ToolTip> 
</Button.ToolTip> 
</Button> 
    </Grid> 
</Page> 

你可以把它粘贴到Kaxaml,并对其进行测试。

希望这会有所帮助。

+0

谢谢,Anvaka。一个注意:我实际上希望模板内容绑定到' ToolTip.Content',但那是简单的在你的代码来完成与'',再次感谢。 – Gregyski 2009-09-02 19:10:14

+0

同时,感谢您对Kaxaml头了。到目前为止,我喜欢它很多比XamlPad。 – Gregyski 2009-09-02 19:52:25

+0

你总是欢迎:)。我很高兴我能帮助:)。干杯。 – Anvaka 2009-09-02 20:16:24