microsoft / microsoft/microsoft-ui-xaml

The exception 'Element is already the child of another element' is thrown when switching between tabs

Open
#10,585 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

The exception 'Element is already the child of another element' is thrown when I use a DependencyObject as the DataContext for the TabView.TabItemTemplate.

The problem does not occur if RibbonTab does not inherit from DependencyObject.

I'm migrating my project from WPF to WinUI. I'm creating a simplified analogue of Ribbon using a TabView with an overridden template. I needed to pass UI elements for display in tabs. For this purpose, I created a proxy class RibbonTab, which inherits from DependencyObject, with a property called Children.

Template:
```xml











```

Usage:
```xml






```

The issue occurs when switching between tabs. Is it a bug or am I doing something wrong?

### Steps to reproduce the bug

1. Open solution [WinUITest.zip](https://github.com/user-attachments/files/20476531/WinUITest.zip)
2. Run app
3. Switch to second tab

### Expected behavior

_No response_

### Screenshots

_No response_

### NuGet package version

WinUI 3 - Windows App SDK 1.7.2: 1.7.250513003

### Windows version

Windows 10 (21H2): Build 19044

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by running the WinUITest.zip reproduction and switching from the first to the second tab. Inspect the TabView.TabItemTemplate, RibbonTab DependencyObject, and ItemsRepeater binding, then compare behavior when RibbonTab no longer inherits from DependencyObject. Done means identifying whether this is a WinUI bug or invalid usage and documenting or fixing the confirmed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, csharp
Domain
desktop, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.