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. 我放在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>
    

但我收到错误

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

我做错了什么?

4

3 回答 3

13

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

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

编辑:另外,您的 UserControl 中没有控件,您应该将其拆分为资源(样式、模板等)和控件。您已经为按钮定义了样式。但是目前没有按钮(这就是Grid默认模板中有 a 的原因)。

<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 中使用您的模板和样式,并将样式指定为资源以使用 HyperlinkBut​​ton。

于 2013-09-11T20:03:34.037 回答
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>
于 2014-05-13T09:44:48.927 回答
0

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

于 2013-09-12T05:37:05.093 回答