I have created a custom control but I can’t bind a property to the content like this :
<Style TargetType="control:Pie">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="control:Pie">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentControl Content="{Binding Path=Test}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And in my control
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register(
"Test",
typeof(string),
typeof(Pie),
new PropertyMetadata(null));
When I create the control a set a string in the Test property but nothing appears in the view…
TemplateBindingis used to bind to the templated control and therefore only works inControlTemplate. Just 2 Lines above you already used TemplateBinding for Background, BorderBrush and BorderThickness.Bindingon the otherhand binds to theDataContext, which is an object used to take “business” related data from the user. It should be independant of how your controls works andAs a rule of thumb: If you use a normal binding in a
ControlTemplate, which doesn’t have aRelativeSource,ElementNameorSourceset, it usually should not appear in a ControlTemplate or Style.