【发布时间】:2011-08-28 19:05:51
【问题描述】:
我有一个简单的 XAML 文件,其中有一个网格和文本框。但是当使用我的代码时,它没有通过迭代找到文本框。
VB.Net:
Dim ctl As FrameworkElement = Me.MainWindow
代码:
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>
编辑 问题解决了。我在管理插件中的语言时使用了代码。但是没有调用 MainWindow1.Loaded。如果未加载 Window,则此代码不起作用。如果加载了 Window,则此代码有效。
【问题讨论】:
-
什么代码定义了
ctl? -
基础是主窗口。我想通过迭代接收所有控件的列表。问题不是如何迭代,而是如何接收frameworkelement的children列表。
标签: wpf vb.net xaml controls iterator