Reputation: 1309
I am trying to create linkbutttonlike like said in answer here: How do I make a WPF button look like a link?
<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>
I placed ControlTemplate
inside:
<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>
But I am getting error
The property "Content" can only be set once.
What I am doing wrong?
Upvotes: 9
Views: 30108
Reputation: 95
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>
Upvotes: 2
Reputation: 2123
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.
Upvotes: 0
Reputation: 2999
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.
Try to add <UserControl.Resources>
Tags.
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 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.
Upvotes: 13