C#
Файл MyControlTitleContent.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MyControls
{
public class MyControlTitleContent : Control
{
// dependency properties
public static readonly DependencyProperty MyTitleProperty = DependencyProperty.Register(nameof(MyTitle), typeof(string),
typeof(MyControlTitleContent), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register(nameof(MyText), typeof(string),
typeof(MyControlTitleContent), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty MyBackgroundProperty = DependencyProperty.Register(nameof(MyBackground), typeof(Brush),
typeof(MyControlTitleContent), new FrameworkPropertyMetadata(Brushes.Gray, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static readonly DependencyProperty MyFontSizeProperty = DependencyProperty.Register(nameof(MyFontSize), typeof(double),
typeof(MyControlTitleContent), new FrameworkPropertyMetadata(16.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
// properties
[Bindable(true)]
[Category("Data"), Description("My title")]
public string MyTitle
{
get => (string)GetValue(MyTitleProperty);
set => SetValue(MyTitleProperty, value);
}
[Bindable(true)]
[Category("Data"), Description("My text")]
public string MyText
{
get => (string)GetValue(MyTextProperty);
set => SetValue(MyTextProperty, value);
}
[Category("Appearance")]
public Brush MyBackground
{
get => (Brush)GetValue(MyBackgroundProperty);
set => SetValue(MyBackgroundProperty, value);
}
[Category("Appearance")]
public double MyFontSize
{
get => (double)GetValue(MyFontSizeProperty);
set => SetValue(MyFontSizeProperty, value);
}
// constructor
static MyControlTitleContent()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControlTitleContent), new FrameworkPropertyMetadata(typeof(MyControlTitleContent)));
}
}
}
Xaml
Файл 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:MyControls">
<ControlTemplate x:Key="My.Element.TitleContent" TargetType="local:MyControlTitleContent">
<Border BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- show title -->
<TextBlock
Text="{TemplateBinding MyTitle}"
FontSize="{TemplateBinding MyFontSize}">
</TextBlock>
<!-- show text -->
<Border Grid.Row="1" BorderThickness="0 1 0 0" BorderBrush="Black" Background="{TemplateBinding MyBackground}">
<TextBlock
Text="{TemplateBinding MyText}"
FontSize="{TemplateBinding MyFontSize}">
</TextBlock>
</Border>
</Grid>
</Border>
</ControlTemplate>
</ResourceDictionary>
Xaml
Файл MainWindow.xaml
<Window x:Class="WpfAppMyControl.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:MyControls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!-- add my library -->
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<!-- my control -->
<local:MyControlTitleContent
Template="{StaticResource My.Element.TitleContent}"
MyTitle="My books"
MyText="Fantastic, Science"
Width="260" Height="100"
MyBackground="Yellow"
MyFontSize="16">
</local:MyControlTitleContent>
<!-- my control -->
<local:MyControlTitleContent
Template="{StaticResource My.Element.TitleContent}"
MyTitle="My films"
MyText="'House with animals'"
Width="220" Height="100"
MyBackground="LightGreen"
MyFontSize="16"
Margin="5">
</local:MyControlTitleContent>
</StackPanel>
</Window>