C# 如何创建 WPF 控件模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18750120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create WPF ControlTemplate
提问by Sreginogemoh
Hi guys I am trying to create linkbutttonlike like said in answer here: How do I make a WPF button look like a link?
嗨,伙计们,我正在尝试创建链接按钮,就像这里的回答中所说的那样:如何使 WPF 按钮看起来像链接?
1) I did created UserControl, default xaml looking like that:
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) I placed ControlTemplate
inside
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>
But getting error The property "Content" can only be set once.
但是得到错误属性“Content”只能设置一次。
Looks like I ma doing something wrong?
看起来我做错了什么?
回答by Herm
You can not define Styles or Templates in your UserControl, you have to define it it's resources. Furthermore most controls can only have one Content. The Content of your UserControl is one ControlTemplate and one Style, this is not allowed because the interpreter does not know which one can be assigned as content.
您不能在 UserControl 中定义样式或模板,您必须定义它的资源。此外,大多数控件只能有一个内容。您的 UserControl 的 Content 是一个 ControlTemplate 和一个 Style,这是不允许的,因为解释器不知道可以将哪一个分配为内容。
Try to add <UserControl.Resources>
Tags.
尝试添加<UserControl.Resources>
标签。
edit:
also, your UserControl has no Controls in it, you should split it into resources (Styles, Templates etc.) and Controls. You have defined the styles for your button. But there's no button currently (that's the reason there was a Grid
in the default template).
编辑:此外,您的 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>
technically, you would not need an own user control. you could use your template and style in a ResourceDictionary and assign the Style as Resource to use the HyperlinkButton.
从技术上讲,您不需要自己的用户控件。您可以在 ResourceDictionary 中使用您的模板和样式,并将样式指定为 Resource 以使用 HyperlinkButton。
回答by Programmer
When the ControlTemplate tag is directly under the tag, it's being reffered as the Content of the UserControl. You need to add UserControl.Template tag and then put the ControlTemplate tag.
当 ControlTemplate 标记直接位于标记下方时,它被称为 UserControl 的内容。您需要添加 UserControl.Template 标签,然后放置 ControlTemplate 标签。
回答by KaluSinghRao
Try this it is accurately working.
试试这个它是准确的工作。
<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>