I have a simple control that I've created. In the control's template, there is a rectangle whose VisualBrush's Visual is bound to a dependency property in the control.
I instantiate the control from within a UserControl in my app, and bind the dependency property to an element in my UserControl. The objective is to give a "preview" of some content. I have included the XAML code below to illustrate what I am doing. Anyway, when I do this in my test project, everything wires up correctly. However, when I do this in another project, I hit an exception in a block of code in my control, stating that the dependency property that I bound to an element in my UserControl is NULL. This has me baffled.
Below is the code for my Control Template:
<ControlTemplate x:Key="PanZoomTemplate" TargetType="{x:Type Controls:PanZoom}">
<Grid DataContext="{Binding Path=View, RelativeSource={RelativeSource TemplatedParent}}" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Height="225" Width="202" x:Name="PART_Content" Background="Green" VerticalAlignment="Stretch" HorizontalAlignment="Left">
<Canvas>
<Rectangle x:Name="PART_Visual" Height="{Binding Path=Children[0].ActualHeight}" Width="{Binding Path=Children[0].ActualWidth}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Rectangle.Fill>
<VisualBrush Visual="{Binding}"/>
</Rectangle.Fill>
</Rectangle>
<Thumb x:Name="PART_Thumb" IsHitTestVisible="True" HorizontalAlignment="Left" VerticalAlignment="Top"
Canvas.Left="{Binding Path=ContentOffsetX, RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Canvas.Top="{Binding Path=ContentOffsetY, RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Width="{Binding Path=ThumbWidth, RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Height="{Binding Path=ThumbHeight, RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
Opacity="0.3"
>
<Thumb.Template>
<ControlTemplate
TargetType="{x:Type Thumb}"
>
<Border
BorderBrush="Black"
BorderThickness="1"
Background="Blue"
CornerRadius="1"
/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
</Grid>
<Grid Grid.Row="1" Height="40" Width="200" HorizontalAlignment="Left" VerticalAlignment="Bottom" Background="Transparent">
<Slider
Style="{StaticResource SliderStyle1}"
Minimum="10"
Maximum="250"
Height="28" Value="{Binding Path=ScaleFactor, RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
behaviors:SliderValueChange.Command="{Binding Path=ZoomInCommand, RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="130"
Orientation="Horizontal"/>
</Grid>
</Grid>
</ControlTemplate>
Here is the code in my UserControl, that instantiates the control:
<controls:PanZoom x:Name="PanZoomControl" Template="{StaticResource PanZoomTemplate}" View="{Binding ElementName=ContentPane, Path=SelectedContent}"/>
The element ContentPane is a TabControl in the UserControl.
Lastly, in the Loaded event handler of my control, I am trying to reference the ActualHeight and ActualWidth properties of my View dependency property, that should have been bound already to the ContentPane element; however I hit a null exception:
_unScaledExtent = new Size(View.ActualWidth, View.ActualHeight);
So, in my test project, works great, but in another project with identical code, except the TabControl is being populated with different content, it hits an exception. Thoughts??? What am I doing incorrectly???