I've got a simple control derived from ContentControl
with 3 properties.
My problem comes when I try to perform a control.TransformToVisual() with a control that is placed inside MainContent
. It always brings up an ArgumentNullException
.
My guess is this due to the control having a null Parent property. Is there a simple way to way around this?
C#
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register("LabelText", typeof(string), typeof(LabelledControl), null);
public static readonly DependencyProperty ValidationContentProperty =
DependencyProperty.Register("ValidationContent", typeof(object), typeof(LabelledControl), null);
public static readonly DependencyProperty MainContentProperty =
DependencyProperty.Register("MainContent", typeof(object), typeof(LabelledControl), null);
XAML
<Style TargetType="local:LabelledControl">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:LabelledControl">
<StackPanel Margin="0 10 0 0">
<StackPanel Orientation="Vertical">
<dataInput:Label Content="{TemplateBinding LabelText}" FontWeight="Bold" FontSize="12" IsTabStop="False"/>
<ContentControl Content="{TemplateBinding ValidationContent}" IsTabStop="False"/>
</StackPanel>
<ContentControl x:Name="_contentControl" Content="{TemplateBinding MainContent}" IsTabStop="False"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>