WPF - FrameworkElement的 - 枚举所有decendents?WPF、FrameworkElement、decendents

2023-09-06 08:57:05 作者:心上留下一道伤

我有一个 FrameworkElement的(真的切换按钮)的内容里面,一个弹出

我访问它是这样的:

 切换按钮按钮=(切换按钮)发送;
弹出弹出=(弹出)button.FindName(popSelectIteration);
 

通常能正常工作。但有时弹出空。

我试图找到一种方法来调试这一点。有没有一种方法枚举所有的东西是FindName能找到?

作为背景,这里是如何我的弹出窗口中切换按钮定义的:

 <切换按钮Grid.Column =1的Horizo​​ntalAlignment =右保证金=0,2,0,2选中=btnFindIterationChecked>
 <电网>
  <弹出PlacementTarget ={结合的ElementName = chkIteration}NAME =popSelectIteration关=popSelectIteration_Closed
     AllowsTransparency =真StaysOpen =FALSEPopupAnimation =变脸>
      < BORDER BorderBrush =#FF000000背景=浅蓝色了borderThickness =1,1,1,1CornerRadius =8,8,8,8填充=5>
        <电网>
          < Grid.RowDefinitions>
            < RowDefinition高度=自动>< / RowDefinition>
            < RowDefinition高度=300>< / RowDefinition>
            < RowDefinition高度=自动>< / RowDefinition>
          < /Grid.RowDefinitions>
          < TextBlock的前景=黑>选择目标:LT; / TextBlock的>
          &所述;的ScrollViewer Grid.Row =1>
            < TreeView控件的ItemsSource ={结合IterationTree}SelectedItemChanged =TreeView_SelectedItemChanged>
              < TreeView.ItemTemplate>
                < HierarchicalDataTemplate的ItemsSource ={结合儿童}>
                  < StackPanel的方向=横向>
                    < TextBlock的文本={结合PathEnd}/>
                  < / StackPanel的>
                < / HierarchicalDataTemplate>
              < /TreeView.ItemTemplate>
            < /树视图>
          < /的ScrollViewer>
          <按钮Grid.Row =2背景=浅蓝色CONTENT =清除选择单击=btnClear_Click/>
        < /网格>
      < /边框>
    < /弹出>
  < /网格>
< /切换按钮>
 

解决方案

尝试探听。这是一个伟大的WPF调试工具的一切涉及可视树。

修改:您的特定问题似乎是一个时间问题,但。我想这树没有完全建成还没有与您试图访问一个子元素,虽然它不创建。等待,直到最顶端的元素收到一个装事件。

WPF Elements 搜狗百科

I have a FrameworkElement (really a ToggleButton) that has inside its content a Popup.

I access it like this:

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

Usually this works fine. But sometimes popup is null.

I am trying to find a way to debug this. Is there a way enumerate all the "things" that FindName could find?

As background, here is how my popup is defined in the 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. It is a great WPF debugging utility for everything that concerns the visual tree.

Edit: Your specific problem seems to be a timing issue though. I guess that the tree is not completely constructed yet and you try to access a child element although it is not created. Wait until the topmost element receives a "Loaded" event.