如何创建 WPF 控件模板

本文关键字:WPF 控件 创建 何创建 | 更新日期: 2023-09-27 18:31:02

我正在尝试创建类似链接的按钮,就像这里的回答中所说的那样:如何使 WPF 按钮看起来像链接?

  1. 我创建了用户控件,默认 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. 我把ControlTemplate放在里面:

         <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>
    

但是我收到错误

属性"内容"只能设置一次。

我做错了什么?

如何创建 WPF 控件模板

您不能在UserControl中定义样式或模板,您必须将其定义为资源。此外,大多数控件只能有一个内容。UserControl 的内容是一个控件模板和一个样式,这是不允许的,因为解释器不知道哪一个可以指定为内容。

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

编辑:此外,您的用户控件中没有控件,您应该将其拆分为资源(样式、模板等)和控件。您已经定义了按钮的样式。但是目前没有按钮(这就是默认模板中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>

从技术上讲,您不需要自己的用户控件。您可以在资源字典中使用模板和样式,并将样式指定为资源以使用超链接按钮。

试试这个它正在准确工作。

 <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>

当 ControlTemplate 标记位于标记的正下方时,它被引用为 UserControl 的内容。您需要添加 UserControl.Template 标记,然后放置 ControlTemplate 标记。