Reputation: 471
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...
Upvotes: 0
Views: 538
Reputation: 11051
TemplateBinding
is used to bind to the templated control and therefore only works in ControlTemplate
. Just 2 Lines above you already used TemplateBinding for Background, BorderBrush and BorderThickness.
Binding
on the otherhand binds to the DataContext
, which is an object used to take "business" related data from the user. It should be independant of how your controls works and
As a rule of thumb: If you use a normal binding in a ControlTemplate
, which doesn't have a RelativeSource
, ElementName
or Source
set, it usually should not appear in a ControlTemplate or Style.
Upvotes: 2