Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:40:05+00:00 2026-06-02T00:40:05+00:00

I am trying to get my ToggleButton from a DataTemplate ListBoxItem lbi = this.UnitsListBox.ItemContainerGenerator.ContainerFromItem(obj)

  • 0

I am trying to get my ToggleButton from a DataTemplate

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

lbi is ok (not null).

I would like to do this:

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

But

VisualTreeHelper.GetChildrenCount(lbi) is 0. 

This is my 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:40:07+00:00Added an answer on June 2, 2026 at 12:40 am

    When I copied/pasted your code into a test project, the next item in the visual tree hierarchy below the ListBoxItem was a Border element, not a ContentPresenter (I use Snoop to view the Visual Tree). I suspect that is why your ContentPresenter object is null.

    If you’re interested, I have some VisualTreeHelpers on my blog that would probably make this easier. You can use them like this:

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

    (Old Answer)

    Your containers are probably not generated

    Here’s an example of how to use the StatusChanged event to identify if the containers have been generated or not before running your code

    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;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying get the data from my database but i am not getting
I am trying get content from the link https://....com:8455 where Server's certificate is not
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
Trying to get this expression to work, can someone look at it and tell
I'm trying get some licensing code from AndroidPit.com working, but I get Unable to
I am trying get the html within .event_recur. $(.entry).each(function(){ alert($(this).find(.event_recur).html()); }); <div class=entry> <p
When I'm trying get method from remote webservice it gives me error. My code
I'm trying to get a ToggleButton to switch the text in a TextView back
I'm trying get a list of CNAME records using PHP. This method used to
I'm trying get a member based on a String from a class and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.