Click here to Skip to main content
15,891,828 members

Problem with WPF dynamic nested controls selected item

HugeHugh asked:

Open original thread
I'm having a real problem with WPF not maintaining the current Selected Item when I create a data-bound user control dynamically. The easiest way to explain it is to show you a stripped-down example.

In this simple example I have a WPF window that contains a TabControl named OuterTabs in the XAML markup. The TabControl has a DataTemplate for a local DummyClass, so that it will display a User Control named InnerTabs whenever it has DummyClass as its content.

Here is my MainWindow.xaml:

XML
<Window x:Class="SubTabTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SubTabTest"
        Loaded="Window_Loaded"
        Title="WPF Selection Problem" Height="250" Width="350">
    <TabControl x:Name="OuterTabs">
        <TabControl.Resources>
            <DataTemplate DataType="{x:Type local:DummyClass}">
                <local:InnerTabs Margin="10" DataContext="{Binding}" />
            </DataTemplate>
        </TabControl.Resources>
    </TabControl>
</Window>

Here is my InnerTabs.xaml, the user control:

XML
<UserControl x:Class="SubTabTest.InnerTabs"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TabControl>
        <TabItem Header="Inner Tab 1">
            <TabItem.Content>
                <StackPanel Orientation="Horizontal" Margin="20">
                    <TextBlock Text="SubTab of "></TextBlock>
                    <TextBlock Text="{Binding DummyMember}"></TextBlock>
                </StackPanel>
            </TabItem.Content>
        </TabItem>
        <TabItem Header="Inner Tab 2">
            <TabItem.Content>
                <StackPanel Orientation="Horizontal" Margin="20">
                    <TextBlock Text="SubTab of "></TextBlock>
                    <TextBlock Text="{Binding DummyMember}"></TextBlock>
                </StackPanel>
            </TabItem.Content>
        </TabItem>
    </TabControl>
</UserControl>

InnerTabs.xaml has no significant code behind (just InitializeComponent() in the constructor)

Finally here is my MainWindow.xaml.cs code behind. When the window is loaded, it dynamically creates 2 tabs. For this simple example, the DummyClass is just defined in the MainWindow.xaml.cs

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

namespace SubTabTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TabItem tiNew = new TabItem()
            {
                Header = "Outer Tab 1",
                Content = new DummyClass()
                {
                    DummyMember = 1
                }
            };

            OuterTabs.Items.Add(tiNew);

            tiNew = new TabItem()
            {
                Header = "Outer Tab 2",
                Content = new DummyClass()
                {
                    DummyMember = 2
                }
            };

            OuterTabs.Items.Add(tiNew);
        }
    }

    public class DummyClass
    {
        public int DummyMember
        {
            get;
            set;
        }
    }
}

To demonstrate the bug, follow these steps:

1) Run the app
2) Switch from 1 outer tab to the other outer tab
3) Notice they both have SubTab 1 selected
4) Now switch the current selection of one Inner Tab to the 2nd tab
5) Now change the outer tab selection
6) Notice that its current tab is also now the 2nd tab, but when you last left this tab, it had the 1st tab selected. So when you changed the current tab on the 1st outer tab, it had the effect of ALSO changing the current tab on the 2nd outer tab!

This has also been observed when each Outer Tab contains a Calendar control. If I change the date on Tab 1, it automatically also changes the date on Tab 2! I want both tabs to be able to independently keep their UI state including all selections, etc.

Thank you everyone for your help with this issue.
Tags: C#, .NET (.NET 3.5, .NET4), WPF, User Controls, Selection

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900