Best way to get child in usercontrol content template
Posted: 20 Feb 2014, 23:22
So, say I have a user control like this:
Which is used in XAML like this:
Now, given "Root" as a control reference, I want to find Foo in the hierarchy.
I can find things like "TopBar" given "Window", by using:
But I can't determine how to get something in the Content inside of Window (defined in the second XAML).
Ultimately, my goal is to have a Window user control which allows me to properly place child content inside of the window. I can do this in WPF (using both UserControl templates, as well as, and perhaps more optimally, with custom controls), but the path in Noesis (Unity) is unclear.
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="UI.WindowControl"
x:Name="UserControl"
d:DesignWidth="380" d:DesignHeight="300">
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}" >
<Grid x:Name="Window" HorizontalAlignment="Center" VerticalAlignment="Center" Width="380" Height="300" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1.25" ScaleY="1.25"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Border BorderBrush="#BD000000" BorderThickness="1" Margin="-2">
<DockPanel>
<Grid Height="24" DockPanel.Dock="Top">
<Rectangle x:Name="TopBar" Fill="#FF2584BE"/>
<Button x:Name="CloseButton" Content="X" HorizontalAlignment="Right" VerticalAlignment="Center" Width="22" Margin="0,0,5,0" Foreground="Black" Background="#FF4AAAE4" BorderBrush="{x:Null}" FontFamily="/BlendProject;component/Fonts/#Arial" FontSize="16" FontWeight="Bold" Height="22" Padding="3,0"/>
</Grid>
<Grid x:Name="grid_Body" DockPanel.Dock="Top" Background="White">
<ContentPresenter Margin="4" x:Name="ContentHost"/>
</Grid>
</DockPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Style>
</UserControl>Code: Select all
<Grid x:Name="Root">
<UI:WindowControl x:Name="Window" Margin="210,150">
<Grid Margin="0,-12,0,0" Height="286">
<Button x:Name="Foo" />
</Grid>
</UI:WindowControl>
</Grid>
I can find things like "TopBar" given "Window", by using:
Code: Select all
var topbar = window.GetTemplateChild("TopBar").As<Rectangle>();
Ultimately, my goal is to have a Window user control which allows me to properly place child content inside of the window. I can do this in WPF (using both UserControl templates, as well as, and perhaps more optimally, with custom controls), but the path in Noesis (Unity) is unclear.