c# – 如何创建WPF ControlTemplate

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的C#相关文章