1

我正在尝试从 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>
4

1 回答 1

1

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

如果您有兴趣,我的博客上有一些VisualTreeHelpers可能会使这更容易。你可以像这样使用它们:

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;

    }
}
于 2012-04-16T15:12:26.367 回答