I took your example as a basis, removed a bit too much, and got this example. I removed them to show a minimal example of work.
First, I removed CellBorderThickness
property, as is already attached dependency property.
Son
// Removed
public Thickness CellBorderThickness
{
get { return (Thickness)GetValue(CellBorderThicknessProperty); }
set { SetValue(CellBorderThicknessProperty, value); }
}
In my father control I removed OnApplyTemplate()
, and in function SetBorderThickness()
use the opportunity of attached dependency properties to set value:
Father
// Removed
OnApplyTemplate() { ... }
// Add
Son.SetCellBorderThickness(childControl, value);
Below is a complete example. The structure of example:
XAML
Styles
Son
<Style TargetType="{x:Type SonNamespace:Son}">
<Setter Property="Background" Value="Gainsboro" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding SonNamespace:Son.CellBorderThickness}">
<ContentPresenter Content="I'am a Son"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Father
<Style TargetType="{x:Type FatherNamespace:Father}">
<Setter Property="Background" Value="AliceBlue" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding SonNamespace:Son.CellBorderThickness}">
<ContentPresenter Content="I'am a Father"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
MainWindow
<Grid>
<SonNamespace:Son />
<FatherNamespace:Father SonNamespace:Son.CellBorderThickness="6" />
</Grid>
Code
Son
public class Son : Control
{
public static readonly DependencyProperty CellBorderThicknessProperty =
DependencyProperty.RegisterAttached("CellBorderThickness",
typeof(Thickness), typeof(Son),
new FrameworkPropertyMetadata(new Thickness(2),
FrameworkPropertyMetadataOptions.AffectsRender,
CellBorderThicknessProperty_ChangedCallBack));
public static void SetCellBorderThickness(UIElement obj, Thickness value)
{
obj.SetValue(Son.CellBorderThicknessProperty, value);
}
public static Thickness GetCellBorderThickness(UIElement obj)
{
return (Thickness)obj.GetValue(Son.CellBorderThicknessProperty);
}
private static void CellBorderThicknessProperty_ChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Father father = d as Father;
if (father != null)
{
father.SetBorderThickness((Thickness)e.NewValue);
}
}
}
Father
public class Father : Control
{
private Son childControl;
public void SetBorderThickness(Thickness value)
{
if (childControl != null)
{
Son.SetCellBorderThickness(childControl, value);
}
}
}
Output
Project is available at this link.