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 6913805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:15:52+00:00 2026-05-27T09:15:52+00:00

I have two ListBoxes, one inside another. And both ListBoxes will have items dynamically

  • 0

I have two ListBoxes, one inside another. And both ListBoxes will have items dynamically added into them upon user request.

Each time a button (not shown in the below code snippet) is clicked, a new item is added to the listbox. Each new item includes a new listbox and others.

And I have buttons inside the inner listbox, one for each list item of the inner listbox.

With DataContext, I can find the data binded to the inner list item, and make changes to it, so that changes are reflected on the proper list item.

However, I also need to make changes to the data binded to the outer list item, which corresponds to the button. How can I know which one it is?

I have came up with a solution, which I believe it not elegant enough. By making changes to the model, I can have each inner data holds a reference to the outer data, so that I can find the data binded to the outer list item. This doesn’t seem like a proper solution though. Do you have any suggestions?


Below is code snippet of the xaml. I’ve simplified it, hope it’s easy to understand. I feel you don’t have to read the whole code.

<ListBox Name="QuestionsListBox" HorizontalAlignment="Stretch" ItemContainerStyle="{StaticResource ListItem}" >
    <ListBox.ItemTemplate>
    <DataTemplate>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding Question, Mode=TwoWay}" Grid.Row="0" HorizontalAlignment="Stretch" TextWrapping="Wrap"/>
        <ListBox Name="ChoicesListBox" ItemsSource="{Binding Choices}" Grid.Row="1" HorizontalAlignment="Stretch" ItemContainerStyle="{StaticResource ListItem}">
            <ListBox.ItemTemplate>
            <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" Click="ChoiceAddButton_Click" Height="72" Width="72" HorizontalAlignment="Left" BorderBrush="Transparent">
                    <Button.Background>
                        <ImageBrush ImageSource="/Images/choices.add.png" Stretch="Fill" />
                    </Button.Background>
                </Button>
                <TextBox Text="{Binding Value, Mode=TwoWay}"  HorizontalAlignment="Stretch" Grid.Column="1" Margin="-20,0" />
            </Grid>
            </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    </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-05-27T09:15:53+00:00Added an answer on May 27, 2026 at 9:15 am

    Why not just use QuestionsListBox.DataContext inside ChoiceAddButton_Click directly? You have a direct way to reference the outer ListBox from your code behind since you’ve given it a name, and DataContext is an accessible property.

        private void ChoiceAddButton_Click(object sender, RoutedEventArgs e)
        {
            ...
            var outerLBDataContext= QuestionsListBox.DataContext;
            ...
        }
    

    This works fine for me in a demo solution using your provided XAML.

    Edit 2:

    Sorry, wasn’t thinking. The Button‘s DataContext will be a Choice, not the Choices collection.

    Your inner ListBox‘s DataContext is not a Question, it’s Choices. Your outer TextBox has Question.Question as its DataContext. Binding Text or ItemsSource makes the DataContext point to the binding target. Here is a bit of tricky XAML to sneak in a DataContext reference.

    Add an ElementName to your outer TextBox:

    <TextBox Text="{Binding Question, Mode=TwoWay}" Grid.Row="0" HorizontalAlignment="Stretch" TextWrapping="Wrap" ElementName="questionTextBox"/> 
    

    Now, add a hidden TextBlock inside your inner ListBox:

        <ListBox Name="ChoicesListBox" ItemsSource="{Binding Choices}" Grid.Row="1" HorizontalAlignment="Stretch" ItemContainerStyle="{StaticResource ListItem}"> 
            <ListBox.ItemTemplate> 
            <DataTemplate> 
            <Grid HorizontalAlignment="Stretch"> 
                <Grid.ColumnDefinitions> 
                    <ColumnDefinition Width="Auto" /> 
                    <ColumnDefinition Width="*" /> 
                </Grid.ColumnDefinitions> 
                <Button Grid.Column="0" Click="ChoiceAddButton_Click" Height="72" Width="72" HorizontalAlignment="Left" BorderBrush="Transparent"> 
                    <Button.Background> 
                        <ImageBrush ImageSource="/Images/choices.add.png" Stretch="Fill" /> 
                    </Button.Background> 
                </Button> 
                <TextBox Text="{Binding Value, Mode=TwoWay}"  HorizontalAlignment="Stretch" Grid.Column="1" Margin="-20,0" /> 
                <TextBlock Name="hiddenTextBlock" Visibility="Collapsed" DataContext="{Binding ElementName=questionTextBox, Path=DataContext}"
            </Grid> 
            </DataTemplate> 
            </ListBox.ItemTemplate> 
        </ListBox> 
    

    Finally, inside your event handler, you can navigate around the tree to get that reference:

        private void ChoiceAddButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if(btn == null) return; //won't happen when this method handles the event
            Grid g = btn.Parent as Grid;
            if(g!=null) // also unlikely to fail
            {
               TextBlock tb = g.FindName("hiddenTextBlock") as TextBlock;
               if(tb!=null) // also unlikely to fail, but never hurts to be cautious
               {
                   var currentQuestion = tb.DataContext;
                   // now you've got the DC you want
               }
            }
        }
    

    I’d like to note that this isn’t really an elegant solution. It is an all UI solution, however, which could be a useful thing. But better design would be to include Parent references in your Choice and ChoiceList (or whatever they’re called) classes and use that. Then it would be as simple as calling btn.DataContext.Parent.Parent with appropriate type conversions. This way your code becomes easier to read and maintain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two listboxes that I am dragging one item from to the other.
If I have two listboxes, with a button between them, how do I update
I have two databound listboxes. The first only shows items that have been assigned
I Have two listboxes when I drag One item from the source listbox I
I am trying to achieve some thing one these lines ,I have two listboxes
I have two listboxes that are very close together, one on top and one
I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add
i have two ListBoxes (in a Silverlight 3 Application), each wrapped with a ListBoxDragDropTarget.
I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems
I have created an item swapper control consisting in two listboxes and some buttons

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.