WPF VisualTreeHelper.GetChildrenCount(MyMap) returns 1(边框)当 MyMap.Children.Count = 1237
WPF VisualTreeHelper.GetChildrenCount(MyMap) returns 1 (Border) when MyMap.Children.Count = 1237
在我的 WPF BingMaps 应用程序中,我使用命令“MyMap.Children.Add(image);”
添加了 1237 个“System.Windows.Controls.Image”图像
当我想删除它们时,我尝试使用方法 below.Here 我得到 mychildren result = 1237 但 VisualTreeHelper.GetChildrenCount(MyMap) returns 1(边框类型)。这里有什么问题?谢谢
private void limpaaerop()
{
int mychildren = MyMap.Children.Count; // result = 1237
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MyMap); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(MyMap, i);
if (childVisual is System.Windows.Controls.Image)
{
MyMap.Children.Remove((UIElement)childVisual);
}
}
}
Maingrid 内部 Xaml
</UserControl.Resources>
<Grid x:Name="maingrid" Margin="0,0,0,0">
<Grid.RowDefinitions>
.....
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
....
</Grid.ColumnDefinitions>
<StackPanel x:Name="mapStack" Grid.Row="0" Grid.RowSpan="12" Grid.Column="0" Grid.ColumnSpan="9">
<map:Map x:Name="MyMap" Margin="0,0,0,0" />
</StackPanel>
您应该遍历 MyMap.Children
而不是 VisualTreeHelper.GetChildrenCount(MyMap)
:
private void limpaaerop()
{
int mychildren = MyMap.Children.Count;
for (int i = mychildren - 1; i >= 0; i--)
{
UIElement childVisual = MyMap.Children[i];
if (childVisual is System.Windows.Controls.Image)
{
MyMap.Children.Remove(childVisual);
}
}
}
在我的 WPF BingMaps 应用程序中,我使用命令“MyMap.Children.Add(image);”
添加了 1237 个“System.Windows.Controls.Image”图像当我想删除它们时,我尝试使用方法 below.Here 我得到 mychildren result = 1237 但 VisualTreeHelper.GetChildrenCount(MyMap) returns 1(边框类型)。这里有什么问题?谢谢
private void limpaaerop()
{
int mychildren = MyMap.Children.Count; // result = 1237
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(MyMap); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(MyMap, i);
if (childVisual is System.Windows.Controls.Image)
{
MyMap.Children.Remove((UIElement)childVisual);
}
}
}
Maingrid 内部 Xaml
</UserControl.Resources>
<Grid x:Name="maingrid" Margin="0,0,0,0">
<Grid.RowDefinitions>
.....
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
....
</Grid.ColumnDefinitions>
<StackPanel x:Name="mapStack" Grid.Row="0" Grid.RowSpan="12" Grid.Column="0" Grid.ColumnSpan="9">
<map:Map x:Name="MyMap" Margin="0,0,0,0" />
</StackPanel>
您应该遍历 MyMap.Children
而不是 VisualTreeHelper.GetChildrenCount(MyMap)
:
private void limpaaerop()
{
int mychildren = MyMap.Children.Count;
for (int i = mychildren - 1; i >= 0; i--)
{
UIElement childVisual = MyMap.Children[i];
if (childVisual is System.Windows.Controls.Image)
{
MyMap.Children.Remove(childVisual);
}
}
}