为什么VisualTreeHelper.GetChildrenCount(lbi)== 0?

时间:2012-04-16 14:41:24

标签: wpf xaml

我正试图从DataTemplate获取我的ToggleButton

ListBoxItem lbi = this.UnitsListBox.ItemContainerGenerator.ContainerFromItem(obj) as ListBoxItem;

lbi是好的(不是空的)。

我想这样做:

ContentPresenter cp = VisualTreeHelper.GetChild(lbi, 0) as ContentPresenter;
ToggleButton btn = (ToggleButton) VisualTreeHelper.GetChild(cp, 0);

但是

VisualTreeHelper.GetChildrenCount(lbi) is 0. 

这是我的XAML

<ListBox  MaxWidth="215" FlowDirection="RightToLeft" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"   DockPanel.Dock="Left" Name="UnitsListBox" VirtualizingStackPanel.IsVirtualizing="False" SelectionChanged="UnitsListBox_SelectionChanged" IsSynchronizedWithCurrentItem="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.Resources>
                    <Style TargetType="Button"/>
                </ListBox.Resources>
                <ListBox.ItemTemplate  >
                    <DataTemplate >
                        <ToggleButton x:Name="UnitSidebarButton"  FlowDirection="LeftToRight" Height="60" Width="60"  HorizontalContentAlignment="Center" Background="Transparent"  Margin="0" Padding="0" Checked="UnitSidebarButton_Checked" Unchecked="UnitSidebarButton_Unchecked" Focusable="False" VirtualizingStackPanel.IsVirtualizing="False">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Converter={StaticResource cIMSidebarConverter2}}" TextWrapping="Wrap"  TextAlignment="Center" Background="Transparent"/>
                          </Grid>
                        </ToggleButton>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

1 个答案:

答案 0 :(得分:1)

当我将代码复制/粘贴到测试项目中时,ListBoxItem下面的可视树层次结构中的下一个项目是Border元素,而不是ContentPresenter(我使用{ {3}}查看Visual Tree)。我怀疑这就是你的ContentPresenter对象为空的原因。

如果您有兴趣,我的博客上有一些Snoop可能会让您更轻松。您可以像这样使用它们:

var toggleBtn = VisualTreeHelpers.FindChild<ToggleButton>(lbi);

旧答案

您的容器可能未生成

以下是在运行代码之前如何使用StatusChanged事件来识别容器是否已生成的示例

public Window()
{
    InitializeComponent();

    // Attach StatusChanged event
    UnitsListBox.ItemContainerGenerator.StatusChanged += 
        ItemContainerGenerator_StatusChanged;
}

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    // If containers have been generated
    if (UnitsListBox.ItemContainerGenerator.Status == 
        System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    {
        // Remove event
        UnitsListBox.ItemContainerGenerator.StatusChanged -= 
            ItemContainerGenerator_StatusChanged;

        // Do whatever here
        var lbi = UnitsListBox.ItemContainerGenerator.ContainerFromItem(obj) as ListBoxItem;

    }
}