如何创建WPF ControlTemplate?

9

我将尝试按照这里的回答创建类似链接按钮: 如何使WPF按钮看起来像链接?

  1. 我创建了一个UserControl,默认的xaml如下:
<UserControl x:Class="Wpf.Controls.HyperlinkLikeButtonTemplate"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
            
    </Grid>
</UserControl>
  1. I placed ControlTemplate inside:

         <ControlTemplate x:Key="HyperlinkLikeButtonTemplate" TargetType="{x:Type Button}">
             <TextBlock x:Name="innerText" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Cursor="Hand" >
         <ContentPresenter />
             </TextBlock>
             <ControlTemplate.Triggers>
                 <Trigger Property="Button.IsMouseOver" Value="true">
                     <Setter TargetName="innerText" Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                     <Setter TargetName="innerText" Property="TextDecorations" Value="Underline" />
                 </Trigger>
             </ControlTemplate.Triggers>
         </ControlTemplate>
    
         <Style x:Key="HyperlinkLikeButton" TargetType="{x:Type Button}">
             <Setter Property="Template" Value="{StaticResource HyperlinkLikeButtonTemplate}" />
         </Style>
    

但是我遇到了错误

属性“内容”只能设置一次。

我做错了什么?

3个回答

13

你无法在UserControl中定义样式或模板,必须将其定义为资源。此外,大多数控件只能有一个Content。你的UserControl的Content是一个ControlTemplate和一个Style,这是不被允许的,因为解释器不知道哪个可以分配为Content。

尝试添加<UserControl.Resources>标签。

编辑: 另外,你的UserControl里没有控件,你应该将它分成资源(样式,模板等)和控件。你已经定义了按钮的样式,但当前没有按钮(这就是默认模板中有一个Grid的原因)。

<UserControl x:Class="Wpf.Controls.HyperlinkLikeButtonTemplate"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
  <!-- Resources of your control, dictionaries, styles, etc. -->
  <UserControl.Resources>
    <ControlTemplate x:Key="HyperlinkLikeButtonTemplate" TargetType="{x:Type Button}">
        <TextBlock x:Name="innerText" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Cursor="Hand" >
    <ContentPresenter />
        </TextBlock>
        <ControlTemplate.Triggers>
            <Trigger Property="Button.IsMouseOver" Value="true">
                <Setter TargetName="innerText" Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                <Setter TargetName="innerText" Property="TextDecorations" Value="Underline" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="HyperlinkLikeButton" TargetType="{x:Type Button}">
        <Setter Property="Template" Value="{StaticResource HyperlinkLikeButtonTemplate}" />
    </Style>
  </UserControl.Resources>

  <!-- Controls that are in your UserControl -->
  <Button Style="{StaticResource HyperlinkLikeButton}"/>
</UserControl>

从技术上讲,您不需要拥有自己的用户控件。您可以在ResourceDictionary中使用模板和样式,并将该样式分配为资源以使用HyperlinkButton。


你的 XAML 不是有效的。 - Sreginogemoh
2
你目前没有可用的VS,我已经在你的代码中编辑了资源标签。 - Herm
<UserControl.Resources> 没有关闭。 - Sreginogemoh

2

尝试这个,它完全正常工作。

 <ControlTemplate x:Key="ct" TargetType="{x:Type Button}">
    <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="True" >
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="Foreground" Value="Black"></Setter>
        </Trigger>
      <Trigger Property="IsMouseOver" Value="True" >
            <Setter Property="Background" Value="Green"></Setter>
            <Setter Property="Foreground" Value="Blue"></Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

0

当ControlTemplate标签直接位于UserControl标签下方时,它被称为UserControl的内容。您需要添加UserControl.Template标签,然后再放置ControlTemplate标签。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接