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?
The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.
A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).
Create a new WPF project and then right-click on your solution and select Add > New Item... It will open the following window. Now select Custom Control (WPF) and name it MyCustomControl. Click the Add button and you will see that two new files (Themes/Generic.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With