开发者

How to iterate controls in WPF

开发者 https://www.devze.com 2023-03-06 19:47 出处:网络
I have a simple XAML file with a grid in it and textboxes. But when using my code it does not find the textboxes by iteration.

I have a simple XAML file with a grid in it and textboxes. But when using my code it does not find the textboxes by iteration.

VB.Net:

Dim ctl As FrameworkElement = Me.MainWindow

Code:

Dim ChildrenCount As Integer = VisualTreeHelper.GetChildrenCount(ctl)
'ChildrenCount is always zero
For i As Integer = 0 To ChildrenCount - 1
    Dim Child As FrameworkElement = VisualTreeHelper.GetChild(ctl, i)
    Call SetLanguageToControls(Keyword, cLanguage, Child)
Next

XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"  Icon="/OUTPUT%20-%20Histogram;component/Sprectrum.ico">
    <Grid x:Name="LayoutRoot">
        <Grid x:Name="SpectrumContent" Margin="8" Height="120" Width="320">
            <Rectangle Grid.Row="0" Grid.Column="1" Opacity="0.5">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Offset="0" Color="Black" />
                        <GradientStop Offset="1" Color="White" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        <TextBlock x:Name="txtRedMin" Text="|Red:" Foreground="Red" FontWeight="Bold" />
        <TextBlock x:Name="txtRedMinValue" Text="000%" />
        </Grid>
    </Grid>
</Window>

EDIT Problem is solved. I used the code while managing language in my plu开发者_如何学编程gins. But MainWindow1.Loaded was not called. If Window is not loaded, this code does not work. If Window is loaded, this code works.


It depends on what control you pass as ctl

If you pass the Window you'll get the Border

If you pass the Grid (Layout) you'll get the child grid.

EDIT

I just noticed this line

Dim ctl As FrameworkElement = Me.MainWindow

Change it into:

Dim ctl As FrameworkElement = Me

EDIT2

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim ctl As FrameworkElement
    ctl = LayoutRoot
    Dim ChildrenCount As Integer = VisualTreeHelper.GetChildrenCount(ctl)
    For i As Integer = 0 To ChildrenCount - 1
        Dim Child As FrameworkElement = VisualTreeHelper.GetChild(ctl, i)
        Debug.WriteLine(Child.ToString() + ": " +
                        VisualTreeHelper.GetChildrenCount(Child).ToString())
    Next
End Sub


You have to call your function recursively for each child control. That is, in the following line you get a child of ctl:

Dim Child As FrameworkElement = VisualTreeHelper.GetChild(ctl, i)

After this, you need to iterate over children of Child.


The VisualTreeHelper.GetChild method does not recurse. If you want recursion try using Linq-To-VisualTree, e.g.

var textBlocks = ctl.Descendants<TextBlock>();
0

精彩评论

暂无评论...
验证码 换一张
取 消