I am trying make my own version of a control, say a TextBox
, to which I want to add some custom UI. I am want to inherit from this class, so that the user can still call my special text box in the same way he would call a regular one. I have got the following:
// MySpecialTextBox.cs
public class MySpecialTextBox : TextBox
{
static MySpecialTextBox () { }
/* Some special properties */
}
In XAML:
<Style TargetType="{x:Type MySpecialTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MySpecialTextBox}">
<StackPanel>
<Rectangle Width="100" Height="3" Fill="Pink" /> <!-- My own fancy lay-out -->
<TextBox Text="{TemplateBinding Text}"/> <!-- This text box should 'inherit' ALL the properties from the caller -->
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My problem here is that now, the TextBox
only gets the properties you explicitly set in this template, in this case, only Text
. I would like to also bind to Background
, Foreground
, and every other possible property. Obviously I could do something like:
<ControlTemplate TargetType="{x:Type MySpecialTextBox}">
<StackPanel>
<Rectangle Width="100" Height="3" Fill="Pink" />
<TextBox Text="{TemplateBinding Text}"
Background="{TemplateBinding Background}"
Foreground="{TemplateBinding Foreground}"
AcceptsReturn="{TemplateBinding AcceptsReturn}"
AllowDrop="{TemplateBinding AllowDrop}"
<!-- ETCETERA -->
/>
</StackPanel>
</ControlTemplate>
listing all properties, but that feels horribly inefficient. Is there a way to bind to the entire parent in one go?