c# – 如何创建WPF ControlTemplate
发布时间:2020-12-15 18:00:36 所属栏目:百科 来源:网络整理
导读:嗨伙计们,我正在尝试创建linkbutttonlike,如回答这里所说: How do I make a WPF button look like a link? 1)我创建了UserControl,默认的xaml看起来像这样: UserControl x:Class="Wpf.Controls.HyperlinkLikeButtonTemplate" xmlns="http://schemas.micros
|
嗨伙计们,我正在尝试创建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 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
