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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:43:48+00:00 2026-05-13T07:43:48+00:00

I have a TabControl with two TabItem s and a ListBox which is common

  • 0

I have a TabControl with two TabItems and a ListBox which is common to both tabs. This ListBox needs to be aligned inside the tab specific content. Therefore I don’t place it outside of the tabs. I have an another ListBox on one of the tabs. When I select an item in the second ListBox, the first ListBox‘s SelectedItem becomes blue, so I have 2 ListBoxes showen as the focused control at once.

Is there a workaround for this WPF bug? Here is a screenshot and the code:

alt text http://img85.imageshack.us/img85/871/2focusedcontrols.png

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication3
{
    partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void TabControl_SelectionChanged(
            object sender, SelectionChangedEventArgs e)
        {
            var parent = listBox.Parent as Panel;
            parent.Children.Remove(listBox);
            var panel = tabControl.SelectedIndex == 0 ? panel1 : panel2;
            panel.Children.Add(listBox);
        }
    }
}

<Window x:Class="WpfApplication3.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <TabControl Name="tabControl"
                SelectionChanged="TabControl_SelectionChanged">
        <TabItem Header="tab1">
            <StackPanel Name="panel1">
                <ListBox>
                    <ListBoxItem>click me second</ListBoxItem>
                    <ListBoxItem>item</ListBoxItem>
                </ListBox>
                <ListBox Name="listBox">
                    <ListBoxItem>click me first</ListBoxItem>
                    <ListBoxItem>item</ListBoxItem>
                </ListBox>
            </StackPanel>
        </TabItem>
        <TabItem Header="tab2">
            <StackPanel Name="panel2"/>
        </TabItem>
    </TabControl>
</Window>
  • 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-13T07:43:49+00:00Added an answer on May 13, 2026 at 7:43 am

    Hi the problem is that your tab controls selected event will fire when you click on the shared listbox, which brings in the instance of the list box that has focus in the other tab. Its actually not really a bug. It is doing exactly what you told it to do.

    Change your code in your event handler to :

    if (e.Source is TabControl)
    {
       var parent = listBox.Parent as Panel;
       parent.Children.Remove(listBox);
       var panel = tabControl.SelectedIndex == 0 ? panel1 : panel2;
       panel.Children.Add(listBox);
    }
    

    That will stop the undesirable focused behaviour you were seeing.

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

Sidebar

Related Questions

No related questions found

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.