Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create WPF ControlTemplate?

Tags:

c#

wpf

I am trying to create linkbutttonlike like said in answer here: How do I make a WPF button look like a link?

  1. I created UserControl, default xaml looking like that:
<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>
  1. 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?

like image 996
Sreginogemoh Avatar asked Sep 11 '13 19:09

Sreginogemoh


People also ask

What is ControlTemplate?

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.

What is difference between a ControlTemplate and a DataTemplate in WPF?

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).

How do I customize WPF controls?

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.


2 Answers

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>
like image 147
KaluSinghRao Avatar answered Oct 03 '22 20:10

KaluSinghRao


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.

like image 42
Herm Avatar answered Oct 03 '22 19:10

Herm