How to use a derived ControlTemplate in WPF

Posted by Frank Fella on Stack Overflow See other posts from Stack Overflow or by Frank Fella
Published on 2010-03-11T18:37:18Z Indexed on 2010/03/11 18:39 UTC
Read the original article Hit count: 636

Filed under:
|
|
|

The following xaml code works:

<Window x:Class="DerivedTemplateBug.Window1"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="clr-namespace:DerivedTemplateBug"
 Title="Window1" Height="300" Width="300">
 <Button>
  <Button.Template>
   <ControlTemplate>
    <Border BorderBrush="Black" BorderThickness="2">
     <TextBlock>Testing!</TextBlock>
    </Border>
   </ControlTemplate>
  </Button.Template>
 </Button>
</Window>

Now, if you define the following data template:

using System.Windows.Controls;

namespace DerivedTemplateBug
{
 public class DerivedTemplate : ControlTemplate
 {
 }
}

And then swap the ControlTemplate for the derived class:

<Window x:Class="DerivedTemplateBug.Window1"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:local="clr-namespace:DerivedTemplateBug"
 Title="Window1" Height="300" Width="300">
 <Button>
  <Button.Template>
   <local:DerivedTemplate>
    <Border BorderBrush="Black" BorderThickness="2">
     <TextBlock>Testing!</TextBlock>
    </Border>
   </local:DerivedTemplate>
  </Button.Template>
 </Button>
</Window>

You get the following error:

Invalid ContentPropertyAttribute on type 'DerivedTemplateBug.DerivedTemplate', property 'Content' not found.

Can anyone tell my why this is the case?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml