I want create custom control add property Info and InfoTemplate for example.
I define ControlTemplate in generic.xaml using ContetnPresenter for Info property.
When I don't use InfoTemplate it's works fine, but when I apply ItemTemplate the content is represents as class name string. This same template applied to GroupBox work like expected. What do I wrong? I need some extra code in OnApplyTemplate?
Bellow is print-screen my app and sources. Red border is a GroupBox, blue is my Control. Green border is part of DataTemplate.
EDIT: For testing I create class MyGroupBox inherited form GroupBox and override method OnHeaderChanged
public class MyGroupBox : GroupBox
{
protected override void OnHeaderChanged(object oldHeader, object newHeader)
{
//base.OnHeaderChanged(oldHeader, newHeader);
}
}
In that situation GroupBox.Heder behave like my MyCustomControl and display text instead of control. So question is: What should I implement in my control event to work like I want?
MyCustomControl.cs
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication7
{
public class MyCustomControl : ContentControl
{
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public object Info
{
get { return (object)GetValue(InfoProperty); }
set { SetValue(InfoProperty, value); }
}
public DataTemplate InfoTemplate
{
get { return (DataTemplate)GetValue(InfoTemplateProperty); }
set { SetValue(InfoTemplateProperty, value); }
}
public static readonly DependencyProperty InfoProperty =
DependencyProperty.Register(nameof(Info), typeof(object), typeof(MyCustomControl), new PropertyMetadata(null));
public static readonly DependencyProperty InfoTemplateProperty =
DependencyProperty.Register(nameof(InfoTemplate), typeof(DataTemplate), typeof(MyCustomControl), new PropertyMetadata(null));
}
}
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication7">
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<StackPanel>
<TextBlock FontWeight="Bold">Info</TextBlock>
<ContentPresenter ContentSource="Info"/>
<TextBlock FontWeight="Bold">Content</TextBlock>
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MainWindow.xml
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication7"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
DATA_CONTEXT
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="dataTemplate">
<Border BorderBrush="Green" BorderThickness="5">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Border BorderBrush="Red" BorderThickness="4">
<GroupBox HeaderTemplate="{StaticResource dataTemplate}">
<GroupBox.Header>
<TextBlock Text="{Binding}"/>
</GroupBox.Header>
</GroupBox>
</Border>
<Border BorderBrush="Blue" BorderThickness="4">
<local:MyCustomControl InfoTemplate="{StaticResource dataTemplate}">
<local:MyCustomControl.Info>
<TextBlock Text="{Binding}"/>
</local:MyCustomControl.Info>
My content
</local:MyCustomControl>
</Border>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
namespace WpfApplication7
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}