c# – 如何创建WPF ControlTemplate

嗨伙计们,我正在尝试创建linkbutttonlike,如回答这里所说: 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>

2)我把ControlTemplate放在里面

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

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

但是获得错误属性内容”只能设置一次.

看起来我做错了什么?

解决方法

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

尝试添加< 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中使用模板和样式,并将Style指定为Resource以使用HyperlinkBut​​ton.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...