WPF - FrameworkElement - 枚举所有后代?

2024-01-11

我有一个FrameworkElement(真的是一个ToggleButton)其内容中有一个Popup.

我这样访问它:

ToggleButton button = (ToggleButton)sender;
Popup popup = (Popup)button.FindName("popSelectIteration");

通常这工作得很好。但有时弹出窗口为空。

我正在尝试找到一种方法来调试这个问题。有没有办法枚举 FindName 可以找到的所有“东西”?


作为背景,以下是我的弹出窗口在 ToggleButton 中的定义方式:

<ToggleButton Grid.Column="1" HorizontalAlignment="Right" Margin="0,2,0,2" Checked="btnFindIterationChecked">
 <Grid>
  <Popup PlacementTarget="{Binding ElementName=chkIteration}"  Name="popSelectIteration" Closed="popSelectIteration_Closed"
     AllowsTransparency="True" StaysOpen="False" PopupAnimation="Fade">
      <Border BorderBrush="#FF000000" Background="LightBlue" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Padding="5">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="300"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
          </Grid.RowDefinitions>
          <TextBlock Foreground="Black" >Select Destination:</TextBlock>
          <ScrollViewer Grid.Row="1" >
            <TreeView ItemsSource="{Binding IterationTree}" SelectedItemChanged="TreeView_SelectedItemChanged">
              <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                  <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding PathEnd}" />
                  </StackPanel>
                </HierarchicalDataTemplate>
              </TreeView.ItemTemplate>
            </TreeView>
          </ScrollViewer>
          <Button Grid.Row="2" Background="LightBlue" Content="Clear Selection" Click="btnClear_Click"/>
        </Grid>
      </Border>
    </Popup>
  </Grid>
</ToggleButton>

Try Snoop http://blois.us/Snoop/。它是一个很棒的 WPF 调试实用程序,适用于与可视化树相关的所有内容。

Edit:你的具体问题似乎是一个时间问题。我猜树尚未完全构建,您尝试访问子元素,尽管它尚未创建。等到最上面的元素收到“Loaded”事件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF - FrameworkElement - 枚举所有后代? 的相关文章

随机推荐