VisualTreeHelper.GetChildren找不到TabItem的子项

时间:2012-08-24 20:40:46

标签: wpf tabcontrol visualtreehelper

我有以下C#代码来查找DepedendencyObject的子代:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            T childType = child as T;
            if (childType == null)
            {
                foreach (var other in FindVisualChildren<T>(child))
                    yield return other;
            }
            else
            {
                yield return (T)child;
            }
        }
    }

当我遍历底部发布的XAML中的TabItems时,将每个TabItem传递给上面的方法,要求它查找所有Expanders,它什么都不返回。另外,我在附加到每个标签项的Loaded事件的事件处理程序中发出此请求。

                                                                                                                                                                                                                                                                                                                                                                                   
                                        
                                                                             

                                <TextBlock Text="Number of Parts" Grid.Column="0"/>                                   
                                <ComboBox Grid.Column="2"                                              
                                          Margin="0,0,0,2"                                                                                                                                        
                                          />                                                              
                            </Grid>
                        </Expander>
                        <Expander Header="Date/Time Format" 
                  Margin="5,0,5,0"
                   Padding="3,3,0,0"
                  IsExpanded="True" >
                            <Grid Margin="20,4,0,4">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>                                                                  
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Date/Time Format" Grid.Row="0"/>
                                <ComboBox Name="cmbDateTimeFormats"                                             
                                          Grid.Row="0" Grid.Column="2"/>
                            </Grid>
                        </Expander>                           
                    </StackPanel>                       
                </DockPanel>
            </Border>                
        </TabItem>
        <TabItem Header="Profile">
            <Border  >
                <DockPanel LastChildFill="False">
                    <StackPanel DockPanel.Dock="Top">
                        <GroupBox Header="Local" 
                              Margin="5,8" Padding="3,3,0,0"
                              >                                 
                            <Grid Margin="20,4,0,4">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="25"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  />
                                    <ColumnDefinition  />
                                    <ColumnDefinition  />

                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <Button Content="Location..." Grid.Row="0" Name="btnProfLoc"    />
                                <TextBlock Text="{Binding ProfileLocation}" Grid.Row="0" Grid.Column="2"/>

                                <Button Name="btnSaveProfile" Height="25"
                                        Margin="2,5,0,0" Grid.Row="1"                                           
                                        Padding="2,1" >                                    
                                    <StackPanel Orientation="Horizontal">                                       
                                        <TextBlock Text="Save" Margin="5,0"/>
                                    </StackPanel>
                                </Button>

                                <Button Name="btnLoadProfile" Height="25"
                                        Margin="2,5,0,0" Grid.Row="2"                                           
                                        Padding="2,1" >
                                    <StackPanel Orientation="Horizontal">

                                        <TextBlock Text="Load" Margin="5,0"/>
                                    </StackPanel>
                                </Button>

                                <Button Name="btnResetProfile" Height="25"
                                        Margin="2,5,0,0" Grid.Row="3"                                           
                                        Padding="2,1" >
                                    <StackPanel Orientation="Horizontal">                                         
                                        <TextBlock Text="Reset" Margin="5,0"/>
                                    </StackPanel>
                                </Button>                                    
                            </Grid>
                        </GroupBox>                       
                    </StackPanel>
                    <StackPanel 
                    DockPanel.Dock="Bottom" Orientation="Horizontal">

                    </StackPanel>
                </DockPanel>
            </Border>                
        </TabItem>
    </TabControl>

任何猜测我的方法有什么问题?我没有尝试过这个特定的自定义控件,但是这个方法已被用于在另一个自定义控件中查找给定类型的子节点。主要区别是我要找的项目是TabItems的子项。

1 个答案:

答案 0 :(得分:3)

选项卡中的控件似乎不是可视树中TabItem的子控件。他们是TabControl的孩子。

如果您将以下代码添加到您的应用中,您可以看到我的意思..并在选项卡上添加一个按钮,其中包含一个报告按钮路径的点击处理程序。

public string Id(object control)
{
    if (control is UIElement)
    {
        string id = ((UIElement)control).GetValue(AutomationProperties.AutomationIdProperty).ToString();
        id += "(" + control.GetType().Name + ")";
        return id;
    }
    return "not a ui element";
}

private static T FindParent<T>(DependencyObject child)
    where T : DependencyObject
{
    if (child == null) return null; 
    var parent = VisualTreeHelper.GetParent(child);
    return parent as T ?? FindParent<T>(parent);
}

public string Path(object control)
{
    if ( control == null ) return "";
    var path = Id(control);
    var parent = FindParent<FrameworkElement>(control as UIElement);
    if (parent != null ) path = Path(parent) +"/"+ path;
    return path;
}

对于我的应用程序,我得到以下内容:“MainForm(MainPage)/(Grid)/(StackPanel)/ TabControl(TabControl)/(网格)/(网格)/(边框)/(ContentPresenter)/(StackPanel)/按钮(按钮)“

注意TabControl,但没有TabItem。

如果我从TabItem本身连接到事件,我得到以下路径:“MainForm(MainPage)/(Grid)/(StackPanel)/ TabControl(TabControl)/(网格)/(网格)/(TabPanel)/ MyTabItem(TabItem的)“

这表示可视树中的TabItem中不存在项,但是作为TabControl的子项。 (这很糟糕。)注意:当你更改标签时,它们会被虚拟化并实现。