I modified the code present in the accepted answer here to return the first found control of a certain type. But when I try to start traversing from the window itself, VisualTreeHelper.GetChildrenCount
returns 0
, although there is a grid placed on it. The modifications which I made have no effect on the result.
Here's how I call the method:
DockPanel panel = UIHelper.FindFirstChild<DockPanel>(this);
And here is my XAML code:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="216" Width="267">
<Grid>
<DockPanel Height="200" Width="250">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Background="LightBlue">
<Button Content="01" Margin="1 1 15 1"/>
<Button Content="02" Margin="1"/>
<Button Content="03" Margin="1"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="25" DockPanel.Dock="Bottom" Background="LightBlue">
<TextBlock VerticalAlignment="Center">Processing...</TextBlock>
<ProgressBar Value="75" Width="100" Margin="4"/>
</StackPanel>
<Grid>
<TextBlock>Content area</TextBlock>
</Grid>
</DockPanel>
</Grid>
</Window>