【问题标题】:How to create WPF ControlTemplate?如何创建 WPF 控件模板?
【发布时间】:2013-09-16 00:07:00
【问题描述】:

我正在尝试像在此处回答中所说的那样创建链接按钮:How do I make a WPF button look like a link?

  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>
    

但是我遇到了错误

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

我做错了什么?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

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

    尝试添加&lt;UserControl.Resources&gt;标签。

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

    【讨论】:

    • 您的 xaml 无效
    • 你的也没有 ;-D 目前没有可用的 VS,我刚刚在你的代码中编辑了资源标签。
    • &lt;UserControl.Resources&gt;没有关闭
    【解决方案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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        相关资源
        最近更新 更多