【问题标题】:Maintaining default visual properties with ControlTemplate使用 ControlTemplate 维护默认视觉属性
【发布时间】:2009-08-26 21:50:57
【问题描述】:

为了将触发动画应用到我的应用程序中的所有ToolTips,我使用了ControlTemplate。但是,当使用 ControlTemplate 时,ToolTip 会丢失其所有默认视觉属性,我相信,由主题定义。除了那些被我覆盖的属性之外,我怎样才能保留所有属性?

使用以下代码

<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

如您所见,主题默认边框、背景等都没有维护。我不想明确设置这些,因为我希望它们根据用户的主题进行调整。我该如何补救?我是不是走错了路?

【问题讨论】:

  • 另请注意,将BasedOn="{StaticResource {x:Type ToolTip}} 添加到ToolTip 样式没有帮助。

标签: .net wpf xaml controltemplate


【解决方案1】:

您不能从主题样式继承 ControlTemplate。但你不必这样做。使用 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,并进行测试。

希望这会有所帮助。

【讨论】:

  • 谢谢,安瓦卡。注意:我实际上希望将模板内容绑定到ToolTip.Content,但这很容易在您的代码中使用&lt;ContentPresenter Content="{TemplateBinding Content}" /&gt; 完成。再次感谢。
  • 另外,感谢您对 Kaxaml 的关注。到目前为止,我比 XamlPad 更喜欢它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2012-04-26
相关资源
最近更新 更多