wpf scrollviewer+网格和VisualTreeHelper.getChildRencount()
因此,我试图从父级控制( maingrid )中获取所有控件,因此我使用了此操作:
public static List<Visual> GetChildrens(Visual iParent, bool iRecursive)
{
List<Visual> result = new List<Visual>();
iParent.Dispatcher.Invoke((Action)(() =>
{
if (iRecursive)
{
Queue<Visual> toCheck = new Queue<Visual>();
toCheck.Enqueue(iParent);
while (toCheck.Count > 0)
{
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);
toCheck.Enqueue(childVisual);
result.Add(childVisual);
}
toCheck.Dequeue();
}
}
else
{
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(iParent); j++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(iParent, j);
result.Add(childVisual);
}
}
}));
return result;
}
它可以与 grid , stackpanel , stackpanel + 网格, scrollviewer ,但不要 scrollviewer + grids grids :
<Grid x:Name="MainGrid">
<ScrollViewer>
<Grid>
<Label Content="Pizza0" x:Name="Test0_Localize_Content" />
<!--Don't work-->
</Grid>
</ScrollViewer>
<ScrollViewer x:Name="SVName">
<Grid>
<Label Content="Pizza1" x:Name="Test1_Localize_Content" />
<!--Don't work-->
</Grid>
</ScrollViewer>
<ScrollViewer>
<Label Content="Pizza0" x:Name="Test0_Localize_Content" />
<!--Work-->
</ScrollViewer>
<Grid>
<Grid>
<Label Content="Pizza2" x:Name="Test2_Localize_Content" />
<!--Work-->
</Grid>
</Grid>
<StackPanel>
<Grid>
<Label Content="Pizza3" x:Name="Test3_Localize_Content" />
<!--Work-->
</Grid>
</StackPanel>
</Grid>
什么我不明白? (因此要发布我的问题:“看起来您的帖子主要是代码;请添加更多细节”)因此:我如何解决它,我不了解它是WPF和C#。
So I'm trying to get all controls from parent control (MainGrid) for it i use this:
public static List<Visual> GetChildrens(Visual iParent, bool iRecursive)
{
List<Visual> result = new List<Visual>();
iParent.Dispatcher.Invoke((Action)(() =>
{
if (iRecursive)
{
Queue<Visual> toCheck = new Queue<Visual>();
toCheck.Enqueue(iParent);
while (toCheck.Count > 0)
{
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(toCheck.Peek()); j++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(toCheck.Peek(), j);
toCheck.Enqueue(childVisual);
result.Add(childVisual);
}
toCheck.Dequeue();
}
}
else
{
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(iParent); j++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(iParent, j);
result.Add(childVisual);
}
}
}));
return result;
}
And it work with Grid, StackPanel, StackPanel + Grid, ScrollViewer but don't with ScrollViewer + Grids:
<Grid x:Name="MainGrid">
<ScrollViewer>
<Grid>
<Label Content="Pizza0" x:Name="Test0_Localize_Content" />
<!--Don't work-->
</Grid>
</ScrollViewer>
<ScrollViewer x:Name="SVName">
<Grid>
<Label Content="Pizza1" x:Name="Test1_Localize_Content" />
<!--Don't work-->
</Grid>
</ScrollViewer>
<ScrollViewer>
<Label Content="Pizza0" x:Name="Test0_Localize_Content" />
<!--Work-->
</ScrollViewer>
<Grid>
<Grid>
<Label Content="Pizza2" x:Name="Test2_Localize_Content" />
<!--Work-->
</Grid>
</Grid>
<StackPanel>
<Grid>
<Label Content="Pizza3" x:Name="Test3_Localize_Content" />
<!--Work-->
</Grid>
</StackPanel>
</Grid>
What i don't understand? (So to post my question "It looks like your post is mostly code; please add some more details") so: how i can fix it, what i don't understand it's WPF and c#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ScrollViewer控件没有孩子的定义,因为它只能容纳一个元素。
要使ScrollViewer的孩子您必须检查其内容。
对您的代码的最简单修改是:
The ScrollViewer control doesn't have a definition of children because it can only hold one element.
To get the child of the ScrollViewer you have to check its Content.
the easiest modify to your code is: